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

  

 

posted @ 2014-09-12 11:13  ICupid  阅读(2475)  评论(0编辑  收藏  举报