Created
April 9, 2015 18:34
-
-
Save mikaelweave/766ba394128a5ecbe66e to your computer and use it in GitHub Desktop.
SQL logic for comparing two date ranges
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 < |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment