using an if statement in Where Clause
is it possible to use an if staement in a where clause?
i.e.
WHERE (underwritername = 'underwriter')
and (case when inceptiondate is null then开发者_StackOverflow社区
(requestdate >= convert(datetime,'01/10/2009',103))
else
(Inceptiondate >= convert(datetime,'01/10/2009',103))
)
and (requestdate <= convert(datetime,'31/10/2010',103))
basically if one column is null, i need to use an alternative column for selection.
Many Thanks,
Adam
You can do that using the CASE
statement a little different:
WHERE (underwritername = 'underwriter')
and (case when inceptiondate is null
AND requestdate >= convert(datetime,'01/10/2009',103) THEN 1
WHEN inceptiondate IS NOT NULL
AND Inceptiondate >= convert(datetime,'01/10/2009',103) THEN 1 ELSE 0 END = 1)
and (requestdate <= convert(datetime,'31/10/2010',103))
精彩评论