Skip to content

Instantly share code, notes, and snippets.

@mikaelweave
Created April 9, 2015 18:34
Show Gist options
  • Save mikaelweave/766ba394128a5ecbe66e to your computer and use it in GitHub Desktop.
Save mikaelweave/766ba394128a5ecbe66e to your computer and use it in GitHub Desktop.
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 <
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment