Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rokde/561183e5c5cca780a12870f4c5080cb4 to your computer and use it in GitHub Desktop.
Save rokde/561183e5c5cca780a12870f4c5080cb4 to your computer and use it in GitHub Desktop.

Comparing Time Periods or Date Ranges with SQL

TL;DR

SELECT * FROM periods
WHERE range_start <= @check_period_end
  AND range_end >= @check_period_start

The opposite:

SELECT * FROM periods
WHERE NOT (range_start > @check_period_end
          OR range_end < @check_period_start)

The explanation is here: https://stackoverflow.com/a/143568

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment