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

我的数据库当前将“日期”列设置为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

标签:sql,mysql

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值