MySQL yyyy-mm-ddThh:mm:ss.sssZZ到yyyy-mm-dd hh:mm:ss [英] MySQL yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss
问题描述
我想上传包含yyyy-mm-ddThh:mm:ss.sssZ
数据的csv文件.
I want to upload my csv file that contains yyyy-mm-ddThh:mm:ss.sssZ
data.
当我在MySQL中设置DATETIME
类型时,
When I set DATETIME
type in MySQL,
我收到了错误代码1292.
I got error code 1292.
MySQL如何成功上传yyyy-mm-ddThh:mm:ss.sssZ
类型?
MySQL How can I upload yyyy-mm-ddThh:mm:ss.sssZ
type successfully?
推荐答案
首先,您需要将字符串日期转换为有效的日期时间格式(yyyy-mm-dd hh:mm:ss
).
First you need to convert your string dates into a valid date time format (yyyy-mm-dd hh:mm:ss
).
借助STR_TO_DATE()
和DATE_FORMAT()
函数,您可以将这些日期字符串转换为上述所需格式.
With the help of STR_TO_DATE()
and DATE_FORMAT()
functions you can convert those date strings into the above desired format.
现在,您可以安全地将数据类型更改/修改为时间戳/日期时间.
Now you can safely change/modify the datatype to timestamp/datetime.
这是一个演示:
将字符串日期转换为有效的日期格式:
Create table yourtable(
id INT primary key AUTO_INCREMENT,
start varchar(50)
);
INSERT INTO yourtable(start) VALUES('1901-02-03T05:30:00.000Z');
UPDATE yourtable
SET start = DATE_FORMAT(STR_TO_DATE(start,'%Y-%m-%dT%H:%i:%s.000Z'),'%Y-%m-%d %H:%i:%s');
更改数据类型:
ALTER TABLE yourtable MODIFY COLUMN start datetime;
这篇关于MySQL yyyy-mm-ddThh:mm:ss.sssZZ到yyyy-mm-dd hh:mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!