Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

IIF

Example

Output “YES” if the condition is true; otherwise, output “NO”.

SELECT IIF(500<1000‘YES’‘NO’);

Definition and Usage

The IIF() function returns one value if a condition is true, or another value if the condition is false.

Syntax

IIF(conditionvalue_if_truevalue_if_false)

Parameter Values

Parameter

Description

Condition

Mandatory. The value that is to be evaluated or tested.

value_if_true

Optional. The value to be returned if the condition evaluates to true.

value_if_false

Optional. The value to be returned if the condition evaluates to false.

Technical Details

Works in:

 SQL Server (from 2008 onward), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse

More Examples

Example

Return 5 when the condition is true, or 10 when the condition is false.

SELECT IIF(500<1000510);

Example

Check if two strings are identical and return “YES” if they match, otherwise return “NO”.

SELECT IIF(‘hello’ = ‘bye’‘YES’‘NO’);

Example

Output “MORE” if the condition holds true; otherwise, output “LESS”.

SELECT OrderID, Quantity, IIF(Quantity>10‘MORE’‘LESS’)
FROM OrderDetails;