开发者

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜