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.

sql-logical-funtions.htm
Advertisements