What is Alternative to CASE Statement in SQL Server? – IIF Function – Interview Question of the Week #164

Question: Is there a shorter alternative to a simple two-way CASE expression in SQL Server?

A ceramic channel takes one of two simple paths

Answer: IIF. SQL Server has supported it since 2012, and I’m still surprised how rarely I see it used. I like it for a small either-or expression. It doesn’t replace every CASE, and it doesn’t give the optimizer a special shortcut: SQL Server translates it into a CASE expression.

Here are the two forms side by side. Both return a column named Result, and each returns TRUE for this condition.

SELECT CASE WHEN -1 < 1 THEN 'TRUE' ELSE 'FALSE' END AS Result;

SELECT IIF(-1 < 1, 'TRUE', 'FALSE') AS Result;
SSMS CASE and IIF queries each returning TRUE
CASE and IIF both return TRUE.

The important caveat is how SQL handles an unknown condition. If a nullable value makes the condition unknown, IIF takes its false branch, just as the equivalent CASE WHEN ... THEN ... ELSE ... END does. Test this before using either expression to classify missing data.

DECLARE @Amount int = NULL;
SELECT IIF(@Amount > 0, 'Positive', 'Not positive or unknown')
       AS Result;

For two alternatives, choose the form your team reads most easily. Use CASE when several conditions or a more explicit decision are clearer. Both forms follow SQL Server’s data-type precedence rules, so keep the possible return values compatible. IIF can be nested only 10 levels deep, the same limit as CASE. If you write CASE for every two-way choice, what makes it preferable in your codebase?

Two-way choice: IIF or CASE?

IIF is not a faster CASE, it is a shorter one, and an unknown condition still takes the false branch.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Function, SQL Scripts, SQL Server
Previous Post
How to Shrink TempDB Without SQL Server Restart? – Interview Question of the Week #163
Next Post
How to Find SQL Server Deprecated Features Used by the Application? – Interview Question of the Week #165

Related Posts

46 Comments. Leave new

  • IIF takes more cost to execute than CASE …… observed practically today

    Reply
  • hitesh mombharkar
    January 7, 2020 11:42 am

    Case expressions may only be nested to level 10.

    Reply
  • This only works if you have a single “When” statement. In order to use IIF to accomplish the same thing, you will have all different columns, rather than the same column Name with a CASE.

    For example, if you have the following:
    Select
    Case
    When A = B Then ‘First Case’
    When C = D Then ‘Second Case’
    Else ‘Third Case’
    End
    From TableName

    And you modify it to use IIF…
    Select
    IIF(A=B, ‘First Case’, ‘Third Case’)
    , IIF (C=D, ‘Second Case’, ‘Third Case’)
    From TableName

    The second option will give you two separate columns, rather than a dingle column that the CASE statement provides.

    Reply
  • Petio Ivanov
    May 9, 2023 11:44 pm

    Why wouldn’t you just nest the second IIF, to maintain a single column?

    Reply
  • pinal i have one query which have lots of case statements in select section which eventually kills the query performance.
    taking that in mind what should be the alternative

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.