MySQL问题从ISO 8601格式更新DATETIME字段 [英] MySQL issue updating DATETIME Field from ISO 8601 format
问题描述
我有一个带有varchar字段的表,其中包含ISO 8601时间格式,例如2015-01-18t10:00:10z.
I have a table with a varchar field that contains ISO 8601 time format like 2015-01-18t10:00:10z.
我可以使用
select STR_TO_DATE('2015-01-18t10:00:10z','%Y-%m-%dt%H:%i:%s')
并获得正确的结果:
2015-01-18 10:00:10
2015-01-18 10:00:10
当我使用它插入DATETIME字段时,我得到:
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')
错误代码:1292 截断了错误的日期时间值:"2015-01-18t10:00:10z"
Error Code: 1292 Truncated incorrect datetime value: '2015-01-18t10:00:10z'
Optin
是varchar ISO 8601时间列
Optin
is the varchar ISO 8601 time column
Optin1
是我要更新的DATETIME字段.
Optin1
is the DATETIME field I want to update.
任何人都知道为什么这是hapepenig以及如何解决该问题?
Anyone know why this is hapepenig and how to fix the issue?
非常感谢.
推荐答案
您应该在模式中添加'z'
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
这篇关于MySQL问题从ISO 8601格式更新DATETIME字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!