gpt4 book ai didi

sql-server-2005 - 在Where子句中使用if语句

转载 作者:行者123 更新时间:2023-12-04 06:53:44 25 4
gpt4 key购买 nike

是否可以在where子句中使用if语句?

WHERE (underwritername = 'underwriter')
and (case when inceptiondate is null then
(requestdate >= convert(datetime,'01/10/2009',103))
else
(Inceptiondate >= convert(datetime,'01/10/2009',103))
)
and (requestdate <= convert(datetime,'31/10/2010',103))

基本上,如果一列为空,我需要使用替代列进行选择。

非常感谢,

亚当

最佳答案

您可以使用稍微不同的 CASE 语句来做到这一点:

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

关于sql-server-2005 - 在Where子句中使用if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com