简体   繁体   中英

2 date columns between another 2 date columns

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM