MySQL issue updating DATETIME Field from ISO 8601 format

孤街醉人 提交于 2019-12-02 20:29:57

问题


I have a table with a varchar field that contains ISO 8601 time format like 2015-01-18t10:00:10z.

I can use

select STR_TO_DATE('2015-01-18t10:00:10z','%Y-%m-%dt%H:%i:%s') 

and get the correct result:

2015-01-18 10:00:10

When I come to use this to insert into a DATETIME field I get:

update test set optin1 = STR_TO_DATE(optin,'%Y-%m-%dt%H:%i:%s')

Error Code: 1292 Truncated incorrect datetime value: '2015-01-18t10:00:10z'

Optin is the varchar ISO 8601 time column

Optin1 is the DATETIME field I want to update.

Anyone know why this is hapepenig and how to fix the issue?

Many Thanks.


回答1:


You should add the 'z' to your pattern,

update test set optin1 = STR_TO_DATE(optin,'%Y-%m-%dt%H:%i:%sz')

http://sqlfiddle.com/#!9/ea601/1



来源:https://stackoverflow.com/questions/28108605/mysql-issue-updating-datetime-field-from-iso-8601-format

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