What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (2024)

Pinal Dave

SQL Interview Questions and Answers

46 Comments

Question: What is Alternative to CASE Statement in SQL Server?

Answer: IIF Function.

Honestly, when I write this blog post I feel a bit sad that IIF Function has not received necessary adoption since its release in the year 2012. When I am writing this blog post it is the year 2018 and it has been 6 years since IIF function released and I see absolutely very little adoption of this feature.

Let us see the following two examples how CASE statement works and how IIF can be its alternative in some situation. Please note that CASE Statement can be very complicatedin some scenarios and IIF can’t handle all the scenarios, however, most of the time CASE statement written for either this or that scenarios and IIF can be very much handy functions in those situations.

Let us see this the simple case of statement

SELECTCASEWHEN-1<1THEN'TRUE'ELSE'FALSE'END ASResult;

Now let us convert the CASE statement to IIF function

SELECTIIF( -1<1,'TRUE','FALSE')ASResult;

Let us see how both of thequeries work and provides the results.

What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (1)

You can see that in both of the above queries if we have to make a decision between two values, it would be easy to write IIF. However, there is something preventing users to use IIF. I would love to know your opinion why do you not use IIF function instead of CASE statements?

Reference:Pinal Dave (https://blog.sqlauthority.com)

SQL Function, SQL Scripts, SQL Server

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

Related Posts

SQL SERVER – gMSA: The Service Did Not Start Due to a Logon Failure

SQL SERVER – Find Hostname and Current Logged In User Name

SQL SERVER – Stored Procedure are Compiled on First Run – SP taking Longer to Run First Time

46 Comments. Leave new

  • What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (5)

    Nikhil Mittal

    August 22, 2019 2:34 pm

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

    Reply
  • What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (6)

    hitesh mombharkar

    January 7, 2020 11:42 am

    Case expressions may only be nested to level 10.

    Reply
  • What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (7)

    Keith Hay

    February 8, 2023 2:19 am

    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
  • What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (8)

    Petio Ivanov

    May 9, 2023 11:44 pm

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

    Reply
  • What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (9)

    Kamran Shahid

    November 13, 2023 11:49 pm

    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

« Older Comments

Leave a Reply

Pinal Dave is an SQL Server Performance Tuning Expert and independent consultant with over 22 years of hands-on experience. He holds a Masters of Science degree and numerous database certifications.

Pinal has authored 13 SQL Server database books and 69 Pluralsight courses. To freely share his knowledge and help others build their expertise, Pinal has also written more than 5,700 database tech articles on his blog at https://blog.sqlauthority.com.

Pinal is an experienced and dedicated professional with a deep commitment to flawless customer service. If you need help with any SQL Server Performance Tuning Issues, please feel free to reach out at pinal@sqlauthority.com.

Pinal is also a CrossFit Level 1 Trainer (CF-L1) and CrossFit Level 2 Trainer (CF-L2).

Nupur Dave is a social media enthusiast and an independent consultant. She primarily focuses on the database domain, helping clients build short and long-term multi-channel campaigns to drive leads for their sales pipeline.

Is your SQL Server running slow and you want to speed it up without sharing server credentials? In my Comprehensive Database Performance Health Check, we can work together remotely and resolve your biggest performance troublemakers in less than 4 hours.

Once you learn my business secrets, you will fix the majority of problems in the future.

Have you ever opened any PowerPoint deck when you face SQL Server Performance Tuning emergencies? SQL Server Performance Tuning Practical Workshop is my MOST popular training with no PowerPoint presentations and 100% practical demonstrations.

Essentially I share my business secrets to optimize SQL Server performance.

What is Alternative to CASE Statement in SQL Server? - IIF Function - Interview Question of the Week #164 - SQL Authority with Pinal Dave (2024)

FAQs

What is the alternative to IIF in SQL Server? ›

You can replace the IIF function with a CASE statement, and this solution will function mostly the same. The CASE statement operates just like the IF function in Excel, or the IIF function, but is structured a bit differently.

What is the alternative to if statement in SQL? ›

SQL Server CASE statement is equivalent to the IF-THEN statement in Excel. The CASE statement is used to implement the logic where you want to set the value of one column depending upon the values in other columns.

What is the difference between IFF and IIF in SQL? ›

The primary difference between IF and IIF in SQL lies in their usage and syntax. The IF statement is used for control flow in procedural code and requires separate blocks of code for each condition. On the other hand, the IIF function is used within SQL expressions and returns a value directly based on the condition.

What is alternative to case statement in SQL Server? ›

Using the IIF function

With an IIF function, we largely replace a lot of the syntactical sugar from the CASE statement with a few simple comma-seperators to differentiate our arguments. All told, both CASE and IIF get the same job done, but if given the choice, IIF will generally be much simpler to use.

What can I use instead of case? ›

case
  • bag.
  • baggage.
  • basket.
  • bin.
  • box.
  • cabinet.
  • caddy.
  • caisson.

What is a better alternative to if statements? ›

A switch statement is a control flow statement that allows a program to execute a different block of code based on the value of a variable or expression. It is similar to an if statement, but it can be more concise and easier to read in certain cases.

What is the difference between case and IIF in SQL Server? ›

CASE vs IIF

Returns one of two values, depending on whether a condition is true or false. Similar to IF , CASE handles NULL according to the conditions specified in the CASE statement. A comparison with NULL is neither true nor false , but NULL . If the boolean_expression contains NULL , IIF returns NULL .

When to use case instead of if SQL? ›

CASE allows you to evaluate multiple conditions and return different values for each condition. Result: IF statement directly controls the flow of execution in procedural code. CASE expression returns a value based on conditions, often used to modify the output of a query.

What is IIF equal to in SQL Server? ›

SQL Server IIF() Function

The IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.

What type does IIF return? ›

Returns one of two parts, depending on the evaluation of an expression. You can use IIf anywhere you can use expressions. You use IIf to determine if another expression is true or false. If the expression is true, IIf returns one value; if it is false, IIf returns another.

What is the difference between IIF and decode function? ›

DECODE is more efficient than using the IIF function in PowerCenter. IIF will evaluate all parts of the statement, even if a previous condition is true. The DECODE will stop evaluating as soon as a condition is true.

What is the difference between IIF and case T SQL? ›

CASE vs IIF

Returns a result based on the evaluation of a set of conditions. It's like a series of IF statements in a single SQL command. Returns one of two values, depending on whether a condition is true or false. Similar to IF , CASE handles NULL according to the conditions specified in the CASE statement.

What is coalesce() in SQL? ›

Definition and Usage

The COALESCE() function returns the first non-null value in a list.

What is the alternative to if exists in SQL? ›

An alternative for IN and EXISTS is an INNER JOIN, while a LEFT OUTER JOIN with a WHERE clause checking for NULL values can be used as an alternative for NOT IN and NOT EXISTS.

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 5606

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.