New Logical Function CHOOSE in SQL Server 2012

New Logical Function CHOOSE in SQL Server 2012

As I describes before too that SQL Server 2012 came with so many different powerful functions which makes developer’s life very easy.  I came up with one new logical functions CHOOSE today. If you want to look at earlier article of new functions of SQL Server 2012, have a look at following links:

Function Link to learn
LEAD Click Here
LEG Click Here
CUME_DIST Click Here
PERCENT_RANK Click Here
LAST_VALUE Click Here
FIRST_VALUE Click Here
EOMONTH Click Here
DATEADD Click Here
DATEFROMPARTS Click Here
DATETIMEFROMPARTS Click Here
TIMEFROMPARTS Click Here
PARSE Click Here
TRY_PARSE Click Here
TRY_CONVERT Click Here
ISNULL Click Here
COALESCE Click Here
CONCAT_NULL_YIELDS_NULL Click Here

CHOOSE function accept the list of value along with Index and return the value based on the index passes to function.  Let us see one small example.

 IF OBJECT_ID('tempdb..#FunctionTesting') IS NOT NULL
 DROP TABLE #FunctionTesting

CREATE TABLE #FunctionTesting
 (
 ID INT IDENTITY(1,1)
 ,EmpName VARCHAR(50)
 ,NightShiftDay INT
 )

INSERT INTO #FunctionTesting VALUES
 ('Ritesh Shah',1)
 ,('Rajan Shah',2)
 ,('Bihag Thakar',3)
 ,('Kavan Dhruv',4)
 ,('Paresh Prajapati',5)
 ,('Kalu Bhuva',6)
 ,('Prapa Acharya',7)

SELECT
 CHOOSE(NightShiftDay,'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday') AS NightShiftDay
 ,EmpName
 FROM
 #FunctionTesting
 

Once you will execute above given SELECT query, you will get day name as against day number along with employee name. Here is the output of above query:

Reference: Ritesh Shah

http://Extreme-Advice.com

http://www.sqlhub.com

Note: Microsoft Books online is a default reference of all articles.

One thought on “New Logical Function CHOOSE in SQL Server 2012

Comments are closed.