Last Updated: February 25, 2016
·
445
· zhekanax

Find out that two segments intersecting or not

Lets say you got two segments (dates or other ranges) and want to find out are they intersecting each over:

         a1|-----------|a2
b1|-----------|b2

Or maybe one including another:

a1|-------------------|a2
       b1|----|b2

In that case you can use simple expression instead of tons of if's or case's:

not if(a1 < b1, a2 < b1, b2 < a1)

Or generic SQL version:

not (case (a1 < b1) when 1 then a2 < b1 else b2 < a1 end)

Searching for 'formula':

the process