CHOOSE() – SQL 2012

I was looking at the the SQL 2012 features and one T-SQL feature that attracted me is CHOOSE.

DECLARE @RECORDS TABLE(ID INT IDENTITY(1,1),NAME VARCHAR(10),STATUS INT);
INSERT INTO @RECORDS(NAME,STATUS) VALUES ('A',1), ('B',2), ('C',2), ('D',3);
SELECT *,
CASE STATUS
WHEN 1 THEN 'Working'
WHEN 2 THEN 'Resigned'
WHEN 3 THEN 'InActive'
END As Status_With_Case
FROM @Records;

Here is how you do this with SQL 2012, much simpler and elegant way.

SELECT *,
CHOOSE(STATUS,'Working','Resigned','IsActive') As Status_With_Choose
FROM @Records;

Simple right?

Leave a Reply

Your email address will not be published. Required fields are marked *