bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL References
SQL•SQL References

SQL Server IIF() Function

Concept visual

SQL Server IIF() Function

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

Example

Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE:

Select

IIF(500<1000, 'YES', 'NO');

Definition and Usage

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

Syntax

Iif(

condition, value_if_true, value_if_false )

Parameter Values

Parameter

Description condition

Required. The value to test value_if_true

Optional. The value to return if condition is TRUE

value_if_false

Optional. The value to return if condition is FALSE

Technical Details

Works in:

SQL Server (starting with 2012), Azure SQL Database

More Examples

Example

Return 5 if the condition is TRUE, or 10 if the condition is FALSE:

SELECT IIF(500<1000, 5, 10);

Example

Test whether two strings are the same and return "YES" if they are, or "NO" if not:

SELECT IIF('hello' = 'bye', 'YES', 'NO');

Example

Return "MORE" if the condition is TRUE, or "LESS" if the condition is FALSE:

SELECT OrderID, Quantity, IIF(Quantity>10,
'MORE', 'LESS')

From

OrderDetails;

Previous

❮ SQL Server Functions

Next

Previous

SQL Server CURRENT_USER Function

Next

SQL Server ISNULL() Function