- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - IIF() Function
The SQL IIF() function is used to verify whether the condition is true or false.
It accepts three parameters boolean_expression, true_value and false_value, and returns one of two values, depending on whether the Boolean expression evaluates to true or false in SQL Server. It returns true_value if the condition is true; false_value otherwise.
Note − If the expression is not a boolean expression or invalid expression, the IIF() function throws an error.
Syntax
Following is the syntax of the SQL IIF() function −
IIF( boolean_expression, true_value, false_value )
Parameters
boolean_expression − It is a valid boolean expression.
true_value − It is a value that can be of any data type and is returned when the boolean expression evaluates to true.
false_value − It is a value that can be of any data type and is returned when the boolean expression evaluates to false.
Return value
This function returns a one of two values, depending on the boolean_expression.
Example
In the following example, we are using the SQL IIF() function to verify whether the boolean expression '10>5' is evaluated as true(return 1) or false(return 0).
SELECT IIF(10>5, 1, 0) as Result;
Output
On executing the above program, it will produce the following output −
+-------+ | Result| +-------+ | 1 | +-------+
Example
The following is another example of the SQL IIF() function, here we are using this function to compare the lengths of two strings 'Hello' and 'World' to determine which one is longer.
DECLARE @STR1 VARCHAR(10) = 'Hello', @STR2 VARCHAR(10) = 'World'; SELECT IIF(LEN(@STR1) > LEN(@STR2), @STR1 + ' string is larger', @STR2 + ' string is larger ') as Result;
Output
Following is the output of the above statement −
+-------------------------+ | Result | +-------------------------+ | World string is larger | +-------------------------+
Example
If we pass an invalid boolean_expression, this function throws an error.
In this example, we are using the SQL IIF() function to verify whether the boolean_expression 'a>b' is evaluated to be true(return 'valid') or false(return 'invalid').
SELECT IIF('a>b', 'valid', 'invalid');
Output
The above SQL statement produces the following output −
An expression of non-boolean type specified in a context where a condition is expected, near '('.
Example
You can also pass the table column as an argument to the SQL IIF() function to verify which customer's salaryis greater than the particular amount. Assume we have created a table with the name Customer using the CREATE statement as follows −
CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2));
Now, let's insert some records in to the Customers table using the INSERT statement as shown below −
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'kaushik', 23, 'Kota', 2000.00 ); INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );
The following SQL query verifies which customers have the less salary of 2000 in the Customer's table −
SELECT ID, NAME, SALARY, IIF(SALARY > 2000, 'Salary is greater than 1000', 'Salary is less than 1000') AS RESULT FROM CUSTOMERS;
Output
The above SQL query generate the following output −
+----+----------+---------------+-----------------------------+ | ID | NAME | SALARY | RESULT | +----+----------+---------------+-----------------------------+ | 1 | Ramesh | 2000.00 | Salary is less than 1000 | | 2 | Khilan | 1500.00 | Salary is less than 1000 | | 3 | kaushik | 2000.00 | Salary is less than 1000 | | 4 | Chaitali | 6500.00 | Salary is greater than 1000 | +----+----------+---------------+-----------------------------+
Note − You can also use the IIf() function to check the age of customers, who are older than the specified age criteria.