CipherIS CipherIS - 18 days ago 7
SQL Question

Add Case Statement in Where Clause

I need to add a

case
statement in a
where
clause. I want it to run either statement below depending on the value of TermDate.

Select *
from myTable
where id = 12345
AND TermDate CASE
WHEN NULL THEN
AND getdate() BETWEEN StartDate AND DATEADD(dd, 30, StartDate)
ELSE
AND GETDATE < TermDate
END

Answer

Why not just use an OR condition?

SELECT * 
FROM  myTable
WHEN  id = 12345
AND   ((TermDate IS NULL AND 
        getdate() BETWEEN StartDate AND DATEADD(dd, 30, StartDate)) OR
       GETDATE() < TermDate)
Comments