1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > mysql 设置日期时间格式_MySQL:更改列类型和日期时间格式

mysql 设置日期时间格式_MySQL:更改列类型和日期时间格式

时间:2020-03-05 18:42:09

相关推荐

mysql 设置日期时间格式_MySQL:更改列类型和日期时间格式

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

1/13/ 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

标签:sql,mysql

来源: https://codeday.me/bug/1119/2034540.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。