Slick way to compute intersection of intervals?
443806Sep 23 2009 — edited Sep 23 2009Hello,
I was wondering if there is any slick way of computing this with only SQL. I have two pairs of numbers (could be dates too), and want to compute the intersection length. For example, if the numbers are (1, 5) and (2, 6), the answer is 3, that is 5 - 2. But if the numbers are (1,5) and (2,4), the answer is 2, i.e. 4 - 2 because (2,4) is wholly contained in (1,5).
Of course, I can use conditioning on all possible ordering combinations of the first and second interval's start and end points. But I was wondering if there is any slick way to do this that I have missed. Thanks,
Regards,
SK