UFN_DATES_AREDATESOVERLAPPING

Checks whether two date ranges are overlapping.

Return

Return Type
bit

Parameters

Parameter Parameter Type Mode Description
@RANGEONESTARTS datetime IN
@RANGEONEENDS datetime IN
@RANGETWOSTARTS datetime IN
@RANGETWOENDS datetime IN

Definition

Copy


            create function dbo.UFN_DATES_AREDATESOVERLAPPING
            (
                @RANGEONESTARTS datetime,
                @RANGEONEENDS datetime,
                @RANGETWOSTARTS datetime,
                @RANGETWOENDS datetime
            )
            returns bit
            as
            begin
                declare @OVERLAPPING bit

                set @OVERLAPPING =    case when    ( @RANGETWOENDS between @RANGEONESTARTS and @RANGEONEENDS) or
                                                (@RANGEONEENDS between  @RANGETWOSTARTS and @RANGETWOENDS) or
                                                ( @RANGETWOSTARTS between @RANGEONESTARTS and @RANGEONEENDS) or
                                                (@RANGEONESTARTS between  @RANGETWOSTARTS and @RANGETWOENDS) or

                                                ( @RANGETWOSTARTS is null and @RANGEONESTARTS <=  @RANGETWOENDS)   or
                                                (@RANGEONESTARTS is null and  @RANGETWOSTARTS <= @RANGEONEENDS)   or
                                                ( @RANGETWOENDS is null and @RANGEONEENDS >=  @RANGETWOSTARTS) or
                                                (@RANGEONEENDS is null and  @RANGETWOENDS >= @RANGEONESTARTS) or

                                                (@RANGEONESTARTS is null and  @RANGETWOSTARTS is null) or
                                                (@RANGEONEENDS is null and  @RANGETWOENDS is null) or
                                                (@RANGEONESTARTS is null and @RANGEONEENDS is null) or
                                                ( @RANGETWOSTARTS is null and  @RANGETWOENDS is null) then 1 else 0 end

                return @OVERLAPPING
            end