Jan 2, 2015

IIF and CHOOSE functions in SQL Server

IIF keyword is available in SQL Server 2012 and earlier version. This function take three argument, first argument is Boolean expression which evaluate true or false and return one argument from another two argument. It is just like CASE statement in SQL Server you may say IIF is short form of CASE. 
Syntax:
IIF ( boolean_expression, true_value, false_value )

Example:
SELECT IIF(5>4, 'TRUE','FALSE') as Result

Result
TRUE

How to use IIF in SQL Query

CREATE TABLE #TBL
(
       ID INT IDENTITY(1,1),
       FName VARCHAR(50),
       LName VARCHAR(50),
       TotalOrder INT,
       CreatedDate DATETIME
)

INSERT INTO #TBL(FName,LName,TotalOrder,CreatedDate)
SELECT 'Dilip','Singh',10,'2015/02/12'
UNION ALL
SELECT 'Raj','Kumar',20,'2015/01/10'

SELECT FName,LName,IIF(TotalOrder>10,'Golden','Silver') AS UserType,CONVERT(DATE,CreatedDate) AS CreatedDate FROM #TBL

DROP TABLE #TBL

Result:










In above example we are evaluating about UserType if any user ordered more than 10 time from our site then he is Golden User otherwise user is Silver User.

CHOOSE Function In SQL Server
Choose function was introduced in SQL Server 2012, Choose function returns item at specified index from the list of the value which are available.

Syntax:
CHOOSE ( index, val_1, val_2 [, val_n ] )

Example:
1-SELECT CHOOSE(1,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(2,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(3,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(4,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(5,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(6,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(7,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(8,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(9,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(10,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(11,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
UNION ALL
SELECT CHOOSE(12,'January','February','March','April','May','June','July','August','September','October','November','December')AS MONTH
Result:























 
2-CREATE TABLE #TBL
(
       ID INT IDENTITY(1,1),
       FName VARCHAR(50),
       LName VARCHAR(50),
       TotalOrder INT,
       CreatedDate DATETIME
)

INSERT INTO #TBL(FName,LName,TotalOrder,CreatedDate)
SELECT 'Dilip','Singh',10,'2015/02/12'
UNION ALL
SELECT 'Raj','Kumar',20,'2015/01/10'

SELECT FName,LName,IIF(TotalOrder>10,'Golden','Silver') AS UserType,CHOOSE(DATEPART(MM,CreatedDate),'January','February','March','April','May','June',
  'July','August','September','October','November','December') AS CreatedMonth  FROM #TBL

DROP TABLE #TBL

Result:








In above example we are listing user's account created month.