SQL.Brain.Teaser

While browsing through some stored procedures in my current system today, I came across the following 'complex' WHERE condition:

((StartDate BETWEEN @DateFrom AND @DateTo AND EndDate BETWEEN @DateFrom AND @DateTo) OR
(StartDate BETWEEN @DateFrom AND @DateTo AND EndDate IS NULL) OR
(StartDate BETWEEN @DateFrom AND @DateTo AND EndDate > @DateTo) OR
(StartDate < @DateFrom AND EndDate IS NULL) OR (StartDate < @DateFrom AND EndDate BETWEEN @DateFrom AND @DateTo) OR (StartDate < @DateFrom AND EndDate > @DateTo))


Can you figure out the logic and simplify the code?


PS. I'll post the answer later.

No comments:

Post a Comment

Popular Post