IIF function in SQL Server 2012

Bandi
Posted by Bandi under Sql Server category on | Points: 40 | Views : 281
IIF function is used to do logical operation equivalent to IF...ELSE statement.
SELECT IIF ( -1 < 1, 'TRUE', 'FALSE' ) AS Result;

OutPut
Result
-------

TRUE


IIF function is only available in MSSQL 2012. To do this kind of operation in MSSQL 2008, we can simulate the above operation by using CASE...WHEN statement in single SELECT or IF..ELSE in T-SQL code

SELECT CASE
WHEN -1 < 1 THEN 'TRUE'
ELSE 'FALSE' END AS Result;


OutPut
Result
-------

TRUE


Sample for IIF usage in Queries:
Displaying Gender M as Male; F as Female
DECLARE @details TABLE(name VARCHAR(50), Gender CHAR(1))

INSERT @details VALUES('Chandu','M'),('Deepa','F'),('Sukesh','M'),('Aruna','F'),('Mohana','F'),('Arun',NULL)

SELECT name,IIF(Gender = 'M', 'Male','Female') AS 'Gender' FROM @details

Comments or Responses

Login to post response