我的数据库当前将“日期”列设置为 varchar(20),我的日期格式如下:
1/13/2015 20:00
我想在我的数据库上运行更新以将列类型更改为日期时间并将我当前日期的格式更改为更典型的格式,例如
yyyy-mm-dd hh:mi
这可以在 MySQL 中完成吗?
最佳答案
使用 STR_TO_DATE
功能
This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.
查看转换的日期
select str_to_date(date_column, '%m/%d/%Y %h:%i')
from tablename
确保一切正常然后运行update
语句
update tablename set date_column = str_to_date(date_column, '%m/%d/%Y %h:%i')
最好以datetime
数据类型存储datetime
数据
Alter table tablename modify column date_column datetime
关于MySQL:更改列类型和日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553198/