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?
Asked
Active
Viewed 28 times
0
-
do you have an example for your question? cannot understand the question very well – LONG Apr 12 '17 at 17:23
1 Answers
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
-
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