The easiest way to find the overlap:
IF EXISTS (SELECT 1 FROM table WHERE @myValueLo <= ExistingRangeEnd AND @myValueHi >= ExistingRangeStart)
It can be shown that this works if you compare the condition above with each of the columns in the diagram below:
Existing range: |-------------------| Overlaps: |-------------| |------------| |----------------------------------| |-------------| Not overlaps: |-----| |----|
in all cases of overlap, both of these tests are correct:
- the start date of an existing range is always before the end date of a new range
- end date of existing range after start date of new range
Those that do not intersect fail one or the other of this test.
source share