MySQL使用带有日期文本的字符串列作为日期字段 [英] MySQL Using a string column with date text as a date field

查看:264
本文介绍了MySQL使用带有日期文本的字符串列作为日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行数据库迁移,我目前有一个带有日期的字符串列(而不是为这些列设置最佳的datetime字段)。是否有一个函数可以在MySQL中调用将该字段转换为日期时间,以便我可以使用日期之前的日期或之后的日期功能?

I am doing a database migration and I currently have a string column with dates (versus having the optimal datetime field set for these). Is there a function I can call in MySQL to convert this field to a datetime so I can use the date features such as before this date or after this date?

推荐答案

SELECT  *
FROM    mytable
WHERE   CAST(mydatefield AS DATETIME) >= CAST('2009-01-01' AS DATETIME)

如果你的日期是一些奇怪的格式, MySQL 不明白,使用 STR_TO_DATE

If your dates are in some weird format that MySQL does not understand, use STR_TO_DATE:

SELECT  *
FROM    mytable
WHERE   STR_TO_DATE(mydatefield, '%Y, %d %m') >= STR_TO_DATE('2009, 01 01', '%Y, %d %m')

这篇关于MySQL使用带有日期文本的字符串列作为日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆