Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function

  • Comments posted to this topic are about the item Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function

    Gregory A. Larsen, MVP

  • Thanks Greg, good stuff! I like the addition of IIF in 2012. 🙂

  • Thank you, very helpful.

    In a large development, I would use a only on CASE rather than IIF for readability and uniformity.

  • I was surprised you didn't discuss case inside aggregate functions.

    before PIVOT existed we used something like this:

    select

    entityid

    ,fieldA = max( case when attribute="A" then value else '' end )

    ,fieldB = max( case when attribute="B" then value else '' end )

    ,...

    ,fieldN = max( case when attribute="N" then value else '' end )

    from

    EAVtable

    group by

    entityid

    I've also used case for custom group/order by:

    order by

    case @tcSortField

    when "LastName" then person.LastName

    when "Email" then person.EmailAddress

    etc.

    end

  • Thanks for the great article!

    I just want to make one small comment. I ran the code below:

    [font="Courier New"]SELECT OrderAmt,

    IIF(OrderAmt > 200,

    'High $ Order',

    'Low $ Order') AS OrderType

    FROM MyOrder;[/font]

    and got this error:

    [font="Courier New"]Msg 102, Level 15, State 1, Line 2

    Incorrect syntax near '>'.

    [/font]

    When I Googled the error, it turns out IIF() is not implemented in SQL 2008. I didn't see that noted in the article so it would be helpful to add a note about that at the start of the piece.

    But again, this is a fantastic tutorial! Thanks again!

    - webrunner

    -------------------
    A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
    Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html

  • Hello,

    In your Stairway to T-SQL Beyond The Basics Level 6 Using the CASE Expression and IIF Function covering the Listing 7: Nesting CASE statement lesson. If I was to add to your “MyOrder” table the values;

    ('01-15-2015', 301.00,NULL), and then run your code, the result for my added value would return. Meaning that if the OrderAmt_Category is Null it would be a layaway. I would think that it should be without layaway.

    301.00 NULL300 Dollar Order with Layaway

    USE tempdb

    GO

    SELECT OrderAmt, Layaway,

    CASE

    WHEN OrderAmt < 100 THEN '< 100 Dollar Order'

    WHEN OrderAmt < 200 THEN '100 Dollar Order'

    WHEN OrderAmt < 300 THEN

    CASE

    WHEN Layaway = 'N'

    THEN '200 Dollar Order without Layaway'

    ELSE '200 Dollar Order with Layaway' END

    ELSE

    CASE

    WHEN Layaway = 'N'

    THEN '300 Dollar Order without Layaway'

    ELSE '300 Dollar Order with Layaway' END

    END AS OrderAmt_Category

    FROM MyOrder

    order by OrderAmt;

    -----------Reorder/Change the 2nd and 3rd CASE statements to accomodate the "Null" values correctly

    USE tempdb

    GO

    SELECT OrderAmt, Layaway,

    CASE

    WHEN OrderAmt < 100 THEN '< 100 Dollar Order'

    WHEN OrderAmt < 200 THEN '100 Dollar Order'

    WHEN OrderAmt < 300 THEN

    CASE

    WHEN Layaway = 'Y'

    THEN '200 Dollar Order with Layaway'

    ELSE '200 Dollar Order without Layaway' END

    ELSE

    CASE

    WHEN Layaway = 'Y'

    THEN '300 Dollar Order with Layaway'

    ELSE '300 Dollar Order without Layaway' END

    END AS OrderAmt_Category

    FROM MyOrder

    order by OrderAmt;

    Great article and very helpful

  • Nice article Greg.

    I've had to use CASE expressions quite often recently in a data migration from an old ERP system.

    You not only just taught me a few new tricks but answered lingering questions I had on why some results are returned.

  • Good basic info about CASE. I've never used IIF, always just CASE.

  • webrunner (4/10/2014)


    Thanks for the great article!

    I just want to make one small comment. I ran the code below:

    [font="Courier New"]SELECT OrderAmt,

    IIF(OrderAmt > 200,

    'High $ Order',

    'Low $ Order') AS OrderType

    FROM MyOrder;[/font]

    and got this error:

    [font="Courier New"]Msg 102, Level 15, State 1, Line 2

    Incorrect syntax near '>'.

    [/font]

    When I Googled the error, it turns out IIF() is not implemented in SQL 2008. I didn't see that noted in the article so it would be helpful to add a note about that at the start of the piece.

    But again, this is a fantastic tutorial! Thanks again!

    - webrunner

    Maybe that's why I have never used IIF. We're still on 2008R2.

  • If CASE already does what IIF can do, why was IIF added for 2012?

  • RLilj33 (4/20/2016)


    If CASE already does what IIF can do, why was IIF added for 2012?

    The IIF function you will primarily use to control the flow of steps in a batch or procedure, where you would use CASE to return a value for a column based on the condition. They can both be adjusted to seem like they could be used in similar situations but IIF function has a good bit more power to it.

    Which actually came across this article on the topic and underneath the IIF is actually just a macro for a CASE statement, the IIF function will actually be converted into a CASE statement.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Thanks Shawn - that exactly answers my question.

  • I would suggest using CASE over IIF within a SQL statement (it sounds like IIF can also be used in T-SQL code?), unless IIF is now considered to be part of the ANSI SQL Standard. It makes for portability between database environments easier. (I would say the same about DECODE() if we were talking about an Oracle environment.)

    --=cf

  • Question: Is there an performance advantage to using the "simple" case over the "searched" case?

  • I couldn't understand what exactly would be the use of the following code in real world? :unsure:

    SELECT *

    FROM MyOrder

    WHERE CASE YEAR(OrderDT)

    WHEN 2014 THEN 'Year 1'

    WHEN 2013 THEN 'Year 2'

    WHEN 2012 THEN 'Year 3'

    ELSE 'Year 4 and beyond' END = 'Year 1';

    -----------------------------------------------------------------------
    Known is a DROP, Unknown is an OCEAN.:ermm:

Viewing 15 posts - 1 through 15 (of 21 total)

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