sql 判断两个时间段是否有交集

本文转自CSDN 链接地址:http://blog.csdn.net/dasihg/article/details/8450195  

时间段:starttime_1到endtime_1,starttime_2到endtime_2
SQL语句:where least(endtime_1, endtime_2) > greatest(starttime_1, starttime_2)
解释:least取最小值,greatest取最大值。

创建函数least、greatest

CREATE FUNCTION least
(
 @time1 datetime,
 @time2 datetime
)
RETURNS datetime
AS
BEGIN
declare @ret datetime
 if(@time2>@time1)set @ret=@time1
 else set @ret=@time2
return @ret
END
GO

 

CREATE FUNCTION greatest
(
 @time1 datetime,
 @time2 datetime
)
RETURNS datetime
AS
BEGIN
declare @ret datetime
 if(@time2<@time1)set @ret=@time1
 else set @ret=@time2
return @ret
END
GO

  

 

sql 判断两个时间段是否有交集

上一篇:人脸识别设备从服务器下载设备一直失败的两点原因


下一篇:SqlServer2000下实现行列转换