mikaelsnavy
4/9/2015 - 6:34 PM

SQL logic for comparing two date ranges

SQL logic for comparing two date ranges

FROM - http://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap
Let ConditionA Mean DateRange A Completely After DateRange B
(True if StartA > EndB)

Let ConditionB Mean DateRange A Completely Before DateRange B
(True if EndA < StartB)

Then Overlap exists if Neither A Nor B is true
( If one range is neither completely after the other,
nor completely before the other, then they must overlap)

Now deMorgan's law says that:

Not (A Or B) <=>  Not A And Not B

Which translates to (StartA <= EndB)  and  (EndA >= StartB)

NOTE: This includes conditions where the edges overlap exactly. If you wish to exclude that,
change the >= operators to >, and <= to <