0

I have 4 date columns- From_Date_A, To_Date_A, From_Date_B, To_Date_B. I want to create a flag column to flag anything where any date between From_Date_A and To_Date_A is between From_Date_B and To_Date_B. Is this possible?

ajc101
  • 49
  • 2
  • 9

1 Answers1

1

Using a case expression to check for overlapping date ranges:

select *
  , case when To_Date_A > From_Date_B 
          and To_Date_B > From_Date_A 
        then 1 
        else 0 
        end as Flag
from t

This is assuming that an overlap does not include the initial dates for the range.

If it should, then:

select *
  , case when To_Date_A >= From_Date_B 
          and To_Date_B >= From_Date_A 
        then 1 
        else 0 
        end as Flag
from t

In depth explanation of this overlap check by Charles Bretana

Community
  • 1
  • 1
SqlZim
  • 37,248
  • 6
  • 41
  • 59
  • That is perfect! I appreciate it a lot! Also, since I'm new, how can I show an example with the data I want in columns and rows? – ajc101 Apr 12 '17 at 17:33
  • @ajc101 You can use this to create text tables https://senseful.github.io/web-tools/text-table/ from tab-delimited data (copied from excel or something), and format them as code on stackoverflow – SqlZim Apr 12 '17 at 17:36
  • 1
    @ajc101 Accept an answer if you find it useful. – ITSGuru Apr 14 '17 at 11:20