IIF() in SQL Server

Today, we are going to see IIF function in SQL Server. Honestly speaking, I have never used this function in any of my code so far just because I am very much good with CASE WHEN expressions. However, let us look at some of the usages of IIF in details as part of our learning.

Syntax – IIF(condition,value_if_condition_is_true,value_if_condition_is_false)

Declare @Age int =18
Select IIF(@Age >= 18, 'Adult','Kid')

--Equivalent CASE WHEN representation
Select CASE WHEN @Age >= 18 THEN 'Adult' ELSE 'Kid' END

Remarks
    1. SQL Server internally converts IIF to CASE WHEN expression, hence the rules applies to CASE WHEN expression applies to IIF function as well.

    2. It is to be noted that either of 2nd and 3rd parameter should be a NON NULL value.

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!

One thought on “IIF() in SQL Server”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s