SQL如何在where子句中包含条件

SQL如何在where子句中包含条件,sql,sql-server,Sql,Sql Server,我有一个SQL Server查询,它使用以下方法过滤某些数据: and datepart(mm,cs.timestamp) <= @month 有没有关于如何在同一行中完成的线索?我不确定你是否能在一行中完成。但您可以在多行中执行此操作,如: and ( ( datepart(mm,cs.timestamp) <= @month and @accumulate = true ) or ( datepart(mm.cs.timestamp) = @month and @accu

我有一个SQL Server查询,它使用以下方法过滤某些数据:

and datepart(mm,cs.timestamp) <= @month

有没有关于如何在同一行中完成的线索?

我不确定你是否能在一行中完成。但您可以在多行中执行此操作,如:

and ( ( datepart(mm,cs.timestamp) <= @month and @accumulate = true )
   or ( datepart(mm.cs.timestamp) = @month and @accummulate = false ) )

和((datepart(mm,cs.timestamp)我不确定是否可以在一行中完成。但是可以在多行中完成,如:

and ( ( datepart(mm,cs.timestamp) <= @month and @accumulate = true )
   or ( datepart(mm.cs.timestamp) = @month and @accummulate = false ) )

和((datepart(mm,cs.timestamp)这更简洁一些,可以是一行

(DATEPART(mm,cs.timestamp) = @month 
 OR (@accumulate = 1 AND DATEPART(mm, cs.timestamp) < @month))

这更简洁一点,可以是一行

(DATEPART(mm,cs.timestamp) = @month 
 OR (@accumulate = 1 AND DATEPART(mm, cs.timestamp) < @month))

您应该将存储过程与
IF
THEN
语句一起使用来分隔这两个条件

您应该将存储过程与
IF
THEN
语句一起使用来分隔这两个条件

您可以显示所有代码吗?您最好使用动态SQL。需要考虑的类似问题是:不可以回答你的问题,更多的是对编码风格的评论。你能展示所有的代码吗?你最好使用动态SQL。类似的问题需要考虑:不是回答你的问题,更多的是对编码风格的评论。
SIGN(@month - DATEPART(MONTH, cs.timestamp)) in (0, @accumulate)