关于SQL时间类型的模糊查询

关于SQL时间类型的模糊查询

今天用time Like '2008-06-01%'语句来查询该天的所有数据,被提示语句错误。查了一下才发现该模糊查询只能用于String类型的字段。


自己也查阅了一些资料。关于时间的模糊查询有以下三种方法:

 

1.Convert转成String,在用Like查询。

select * from table1   where convert(varchar,date,120like   '2006-04-01%'  

 

2.Between

select * from table1 where time between '2006-4-1 0:00:00' and '2006-4-1 24:59:59'";

SQL语句根据具体时间查询数据

时间格式为:年/月/日 时:分:秒

例如:2017/2/28 16:23:23

sql语句为:selecte * from A where createtime=to_date('2017/2/28 16:23:23' ,'yyyy/mm/dd hh24:mi:ss');


 

3   datediff ()函数

select   *   from  table1    where   datediff ( day ,time, ' 2006-4-1 ' ) = 0

 

第一种方法应该适用与任何数据类型;

第二种方法适用String外的类型;

第三种方法则是为date类型定制的比较实用快捷的方法。

猜你喜欢

转载自blog.csdn.net/boss2967/article/details/80175071