MySQL中日期与字符串相互转换

1、日期无需转换查询(日期在数据库中的类型为字符串)

select * from day where dateTime > '2016-03-15'

2、使用date_format

select * from day where date_format(dateTime,'%Y-%m-%d') > '2016-03-15'

3、日期转换还可以使用str_to_date()

select str_to_date(dateTime,'%Y-%m-%d %H:%i:%s')

mysql语句中把string类型字段转datetime类型

在mysql里面利用str_to_date()把字符串转换为日期

此处以表h_hotelcontext的Start_time和End_time字段为例,查询当前

时间在此范围之内的数据。  www.2cto.com  

select * from h_hotelcontext where now() between STR_TO_DATE

(Start_time,'%Y-%m-%d %H:%i:%s') and STR_TO_DATE(End_time,'%Y-%m-%d %H:%i:%s'); 

注:'%Y-%m-%d %H:%i:%s'格式为:2012-10-11 16:42:30

发布了90 篇原创文章 · 获赞 92 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/LMAKE_nbsp/article/details/105109229
今日推荐