我的数据库当前将“日期”列设置为varchar(20),我的日期格式如下:

1/13/2015 20:00

我想在数据库上运行更新以将列类型更改为datetime并将当前日期的格式更改为更典型的格式,例如

yyyy-mm-dd hh:mi

可以在MySQL中完成吗?

解决方法:

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 tablename set date_column = str_to_date(date_column, '%m/%d/%Y %h:%i')

将日期时间数据存储为日期时间数据类型更好

Alter table tablename modify column date_column datetime