sql between写法关于查时间区间是否重叠,时间是否在时间段范围内

 
--判断是否重叠
SELECT  *
FROM    TABLEName
WHERE   starttime BETWEEN '2008-10-01' AND '2008-10-07'
        OR endtime BETWEEN '2008-10-01' AND '2008-10-07'
        OR '2008-10-01' BETWEEN starttime AND EndTIme
        OR '200-10-07' BETWEEN starttime AND EndTime;
--日期时间段查询
 --1.如果查询日期参数为'2017/02/21',而数据库表中的字段为'2017/02/21 12:34:16.963',则需要格式化一下日期才能查询出来,如下

select * from table t where t.date between CONVERT(datetime, '2017/02/21', 120) and CONVERT(datetime, '2017/02/21', 120)+' 23:59:59') ;

查询的范围为'2017/02/21 00:00:00'~'2017/02/21 23:59:59',这样就能解决问题。

-- 2.或者使用dateadd方法,把日期加1天,如下

select * from table t where t.date  >= CONVERT(datetime, '2017/02/21')  and t.date < CONVERT(datetime, dateadd(day,1,'2017/02/21'));

查询的范围为'2017/02/21' <= t.date < '2017/02/22'
          
原文地址:https://www.cnblogs.com/shy1766IT/p/5187672.html