SQL条件语句,包含2个显示意外结果的变量

时间:2018-04-14 16:19:37

标签: sql sql-server

我有一个SP需要根据以下内容进行过滤:

....
where 
and cs.chargetype = @chargetype

AND (@chargetype = 'Q' and
    (@biweeklypart = 1 and datepart(dd,cs.scheduledate) = 15)
    OR 
    (@biweeklypart = 1 and datepart(dd,cs.scheduledate) <> 15)
    )

但是没有用。

我需要的是,只有当@chargetype = 'Q'@biweeklypart = 1时,我必须按datepart(dd,cs.scheduledate) = 15进行过滤,如果是@chargetype = 'Q'@biweeklypart = 2那么我必须按datepart(dd,cs.scheduledate) <> 15过滤。

有任何线索吗?我以为我做得对。

1 个答案:

答案 0 :(得分:1)

根据您的说明,您需要:

where cs.chargetype = @chargetype and
      ( (@chargetype = 'Q' and @biweeklypart = 1 and day(cs.scheduledate) = 15) or 
        (@chargetype = 'Q' and @biweeklypart = 2 and day(cs.scheduledate) <> 15) or
        (@chargetype <> 'Q')
      )