Conditional WHERE clause in SQL Server

后端 未结 6 1561
猫巷女王i
猫巷女王i 2020-11-22 12:43

I am creating a SQL query in which I need a conditional where clause.

It should be something like this:

SELECT 
    DateAppr,
    Time         


        
6条回答
  •  日久生厌
    2020-11-22 12:57

    The problem with your query is that in CASE expressions, the THEN and ELSE parts have to have an expression that evaluates to a number or a varchar or any other datatype but not to a boolean value.

    You just need to use boolean logic (or rather the ternary logic that SQL uses) and rewrite it:

    WHERE 
        DateDropped = 0
    AND ( @JobsOnHold = 1 AND DateAppr >= 0 
       OR (@JobsOnHold <> 1 OR @JobsOnHold IS NULL) AND DateAppr <> 0
        )
    

提交回复
热议问题