Conditional Function IIF – SQL Server

Depending on whether the Boolean expression in SQL Server evaluates to true or false, the conditional function IIF returns one of two values. For ease of comprehension, the syntax and examples are provided below.

Syntax: IIF (boolean_expression, true_value, false_value)

Examples:
SELECT IIF( 1=2, ‘TRUE’, ‘FALSE’);
Result: FALSE
SELECT IIF( 1=2, ‘RIGHT’, ‘WRONG’)
Result: WRONG

Let’s see how it will be used in the table’s data. Consider the popular ’emp’ table.

SELECT Ename,
Sal,
IIF(Sal<1000,’Lower’,’Higher’),
Comm,
IIF(Comm IS NULL OR Comm=0,’None’,’Provided’)
FROM emp;

Result:
IIF_SQLServer

Hope you find this article helpful.

Please subscribe for more interesting updates.

2 comments

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 )

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