SQL子句在where子句中

时间:2017-06-27 18:49:19

标签: sql sql-server

您好我的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,则希望按datepart(mm,cs.scheduledate) = @month过滤

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

过滤

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

我不清楚你在问什么,但我有一些想法可以尝试:

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