Conditional WHERE clause in SQL Server

178,538

Solution 1

Try this

SELECT 
    DateAppr,
    TimeAppr,
    TAT,
    LaserLTR,
    Permit,
    LtrPrinter,
    JobName,
    JobNumber,
    JobDesc,
    ActQty,
    (ActQty-LtrPrinted) AS L,
    (ActQty-QtyInserted) AS M,
    ((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
FROM 
    [test].[dbo].[MM]
WHERE 
    DateDropped = 0
    AND (
    (ISNULL(@JobsOnHold, 0) = 1 AND DateAppr >= 0) 
    OR 
    (ISNULL(@JobsOnHold, 0) != 1 AND DateAppr != 0)
    )

You can read more about conditional WHERE here.

Solution 2

Try this one -

WHERE DateDropped = 0
    AND (
        (ISNULL(@JobsOnHold, 0) = 1 AND DateAppr >= 0) 
        OR 
        (ISNULL(@JobsOnHold, 0) != 1 AND DateAppr != 0)
    )

Solution 3

To answer the underlying question of how to use a CASE expression in the WHERE clause:

First remember that the value of a CASE expression has to have a normal data type value, not a boolean value. It has to be a varchar, or an int, or something. It's the same reason you can't say SELECT Name, 76 = Age FROM [...] and expect to get 'Frank', FALSE in the result set.

Additionally, all expressions in a WHERE clause need to have a boolean value. They can't have a value of a varchar or an int. You can't say WHERE Name; or WHERE 'Frank';. You have to use a comparison operator to make it a boolean expression, so WHERE Name = 'Frank';

That means that the CASE expression must be on one side of a boolean expression. You have to compare the CASE expression to something. It can't stand by itself!

Here:

WHERE 
    DateDropped = 0
    AND CASE
            WHEN @JobsOnHold  = 1 AND DateAppr >= 0 THEN 'True'
            WHEN DateAppr != 0 THEN 'True'
            ELSE 'False'
        END = 'True'

Notice how in the end the CASE expression on the left will turn the boolean expression into either 'True' = 'True' or 'False' = 'True'.

Note that there's nothing special about 'False' and 'True'. You can use 0 and 1 if you'd rather, too.

You can typically rewrite the CASE expression into boolean expressions we're more familiar with, and that's generally better for performance. However, sometimes is easier or more maintainable to use an existing expression than it is to convert the logic.

Solution 4

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
    )

Solution 5

Often when you use conditional WHERE clauses you end upp with a vastly inefficient query, which is noticeable for large datasets where indexes are used. A great way to optimize the query for different values of your parameter is to make a different execution plan for each value of the parameter. You can achieve this using OPTION (RECOMPILE).

In this example it would probably not make much difference, but say the condition should only be used in one of two cases, then you could notice a big impact.

In this example:

WHERE 
    DateDropped = 0
    AND (
    (ISNULL(@JobsOnHold, 0) = 1 AND DateAppr >= 0) 
    OR 
    (ISNULL(@JobsOnHold, 0) <> 1 AND DateAppr <> 0)
    )
OPTION (RECOMPILE)

Source Parameter Sniffing, Embedding, and the RECOMPILE Options

Share:
178,538
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

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

    It should be something like this:

    SELECT 
        DateAppr,
        TimeAppr,
        TAT,
        LaserLTR,
        Permit,
        LtrPrinter,
        JobName,
        JobNumber,
        JobDesc,
        ActQty,
        (ActQty-LtrPrinted) AS L,
        (ActQty-QtyInserted) AS M,
        ((ActQty-LtrPrinted)-(ActQty-QtyInserted)) AS N
    FROM 
        [test].[dbo].[MM]
    WHERE 
        DateDropped = 0
                --This is where i need the conditional clause 
        AND CASE
                WHEN @JobsOnHold = 1 THEN DateAppr >=  0
                ELSE  DateAppr != 0
            END
    

    The above query is not working. Is this not the correct syntax or is there another way to do this that I don't know?

    I don't want to use dynamic SQL, so is there any other way or do I have to use a workaround like using if else and using the same query with different where clauses?

  • Admin
    Admin almost 11 years
    Just a query According to this [link] (dba.stackexchange.com/a/5337/27862) answer there may be case when the both conditions will get resolved in AND operator use so if first one fails then also it can apply the DateAppr= 0 condition right ?
  • Admin
    Admin almost 11 years
    Just a query According to this[link](dba.stackexchange.com/a/5337/27862) answer there may be case when the both conditions will get resolved in AND operator use so if first one fails then also it can apply the DateAppr= 0 condition right ?
  • ypercubeᵀᴹ
    ypercubeᵀᴹ almost 11 years
    Not sure what you are asking. The question/answer there is about short-circuiting. You really shouldn't worry about that - unless you have some performance problem.
  • ypercubeᵀᴹ
    ypercubeᵀᴹ almost 11 years
    The OP had changed the code - a less than trivial change - and made our answers look rather dumb :)
  • GaTechThomas
    GaTechThomas over 9 years
    As your table grows, you'll want to watch performance of the query that you end up with, because the optimizer can become pretty unhappy with these types of queries.
  • KyleMit
    KyleMit about 5 years
    Instead of (ISNULL(@JobsOnHold, 0) = 1 you could use @JobsOnHold IS NULL or IS NOT NULL
  • Shaiju T
    Shaiju T over 3 years
    @GaTechThomas , Any solution ?
  • GaTechThomas
    GaTechThomas over 3 years
    @shaijut these sorts of things tend to be indicators of need for a different approach. Consider a materialized view that specifically serves the query at hand, or, if you have the flexibility, an approach that copies the data over into a separate area that gives the read side more options (such as CQRS).