日期模糊查询的三种方法和部分优缺点

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenguanghan123/article/details/85040122

今天因为有需要用到模糊查询的地方所以就在网上找了一下关于模糊查询的方法找到了一下的这几中方法


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

select * from table1 where convert(varchar,date,120) like '2018-12-16%'

select * from table1 where convert(varchar,date,120) like '2018-12-16%'

//这种方法放在数据库中和C#中是可以实现的,其中covert(varchar,date,120) 是
将字段date强制转换成了字符串类型,而后面的‘2018-02-16%’就有点坑了,这是
一个你要进行模糊查找的日期,而这个日期是确定的不能用变量,不能用实体。就只能
是字符串




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

//第二种方法我没有用但是写的也差不多
SELECT sum(cash) from LineLog_Info where OnDate>@date1 and OffDate <@date2
//其中@date1和@date2都是变量,很简单很实用



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

这第三种方法我也没有用所以我也就不知道了

猜你喜欢

转载自blog.csdn.net/chenguanghan123/article/details/85040122