繁体   English   中英

SQL条件在where子句中

[英]SQL conditional in where clause

嗨,我的where子句具有以下条件:

and ( ( datepart(mm,cs.scheduledate) <= @month and @accumulate = 1 )
   or ( datepart(mm,cs.scheduledate) = @month and @accumulate = 0 ) )

因此,当@accumulate = 1我想过滤datepart(mm,cs.scheduledate) <= @month ,如果我有@accumulate = 0@accumulate = 0 datepart(mm,cs.scheduledate) = @month

我做错了什么,因为它总是按datepart(mm,cs.scheduledate) = @month

有什么线索吗?

我不清楚您要问什么,但是我有一些想法可以尝试:

and ( ( datepart(mm,cs.scheduledate) < @month and @accumulate = 1 )
   or ( datepart(mm,cs.scheduledate) = @month and @accumulate IN (0,1) ) )

要么

and 1 = CASE WHEN datepart(mm,cs.scheduledate)  = @month and @accumulate = 0 THEN 1
             WHEN datepart(mm,cs.scheduledate) <= @month and @accumulate = 1 THEN 1 
             ELSE 0 END

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM