IIF and CHOOSE functions in SQL Server


IIF keyword is available in SQL Server 2012 and earlier version. This function takes three arguments, the first argument is the Boolean expression which evaluates true or false and return one case from another two cases. It is just like CASE statement in SQL Server you may say IIF is a 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 the above example, we are evaluating UserType if any user ordered more than 10 times 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 the function returns the item at the 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 the above example, we are listing the user's account created month.

NUnit : Introduction

There are several unit test tools in the market, but one of them is NUnit, the most popular tool for doing unit testing. So in this tutorial...