MySQL yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss [duplicate]

不问归期 提交于 2020-01-24 10:44:45

问题


I want to upload my csv file that contains yyyy-mm-ddThh:mm:ss.sssZ data.

When I set DATETIME type in MySQL,

I got error code 1292.

MySQL How can I upload yyyy-mm-ddThh:mm:ss.sssZ type successfully?


回答1:


First you need to convert your string dates into a valid date time format (yyyy-mm-dd hh:mm:ss).

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.

Here's a demonstration:

SQL FIDDLE DEMO


Convert string dates into valid date format:

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');

Change the datatype:

ALTER TABLE yourtable MODIFY COLUMN start datetime;


来源:https://stackoverflow.com/questions/41042490/mysql-yyyy-mm-ddthhmmss-sssz-to-yyyy-mm-dd-hhmmss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!