本文转自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

  

04-30 10:41