CASE statement in WHERE clause

  • I'm trying to figure out how to use a case statement in a where clause (with !=), here is an idea of what I am trying to do:

    SELECT *
    FROM Table1 JOIN Table2 JOIN Table3
    WHERE

    CASE @Fruit
    WHEN 'Apples' THEN Table1.Fruit = 'Apples'
    WHEN 'Citrus' THEN Table2.Food = 'Oranges' OR Table2.Food = 'Lemons'
    WHEN 'Other' THEN Table1.Fruit != 'Apples' AND Table2.Food != 'Oranges' AND Table2 != 'Lemons'
    END


    I can't find anything online similar to this, does it look right?

  • dan.rowe0898 - Monday, June 12, 2017 11:02 PM

    I'm trying to figure out how to use a case statement in a where clause (with !=), here is an idea of what I am trying to do:

    SELECT *
    FROM Table1 JOIN Table2 JOIN Table3
    WHERE

    CASE @Fruit
    WHEN 'Apples' THEN Table1.Fruit = 'Apples'
    WHEN 'Citrus' THEN Table2.Food = 'Oranges' OR Table2.Food = 'Lemons'
    WHEN 'Other' THEN Table1.Fruit != 'Apples' AND Table2.Food != 'Oranges' AND Table2 != 'Lemons'
    END


    I can't find anything online similar to this, does it look right?

    Suggest you start by reading this article: Catch-all queries by Gail Shaw
    😎

    These types of queries are normally an indicator of poor database design.

  • Hi Dan,

    I don't think it's possible to do the structure you're suggesting - if I understand things correctly, SQL doesn't behave the way you expect it to with regards to boolean expressions.

    Your CASE statement needs to return a value, but you have it returning an condition, which it won't automatically evaluate.

    Here's a similar example, in some languages this would return TRUE or 1, in SQL it is invalid.
    select 1=1
    The CASE statement also expects a simple expression after the THEN (variable/column reference etc.), not a condition

    I recommend using the below format, which is a more standard style.

    WHERE
      @Fruit = 'Apples' AND Table1.Fruit = 'Apples'
      OR @Fruit = 'Citrus' AND (Table2.Food = 'Oranges' OR Table2.Food = 'Lemons')
      OR @Fruit = 'Other' AND Table1.Fruit != 'Apples' AND Table2.Food != 'Oranges' AND Table2.Food != 'Lemons'

    The closest I can get to your sample is below, where the CASE statement returns a value, which is then compared against another value to determine whether to include or exclude the given row, however I see this as less intuitive than the above.

    CASE
         WHEN @Fruit = 'Apples' AND Table1.Fruit = 'Apples' THEN 1
         WHEN @Fruit = 'Citrus' AND (Table2.Food = 'Oranges' OR Table2.Food = 'Lemons') THEN 1
         WHEN @Fruit = 'Other' AND Table1.Fruit != 'Apples' AND Table2.Food != 'Oranges' AND Table2.Food != 'Lemons' THEN 1
      END = 1

    The post Eirikur has mentioned has good advice as to your general approach, I also recommend giving that a read.

    Andrew

  • Please post DDL and inserts.

  • Probably the typical way to do that using a CASE expression would be:


    WHERE
    1 = CASE @Fruit
      WHEN 'Apples' THEN CASE WHEN Table1.Fruit = 'Apples' THEN 1 ELSE 0 END
      WHEN 'Citrus' THEN CASE WHEN Table2.Food = 'Oranges' OR Table2.Food = 'Lemons' THEN 1 ELSE 0 END
      WHEN 'Other' THEN CASE WHEN Table1.Fruit != 'Apples' AND Table2.Food != 'Oranges' AND Table2 != 'Lemons' THEN 1 ELSE 0 END
    END

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Are you comparing to one column or multiple columns? You use Table1.Fruit and Table2.Food even when those appear to represent the same attribute. Please post a most detailed example using DDL, sample data and expected results. Read how to do it in the articles linked in my signature.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply