CHOOSE (Transact-SQL)
SQL Server 2012
Returns the item at the specified index from a list of values in SQL Server 2012.
Returns the data type with the highest precedence from the set of types passed to the function. For more information, see Data Type Precedence (Transact-SQL).
The following example returns the third item from the list of values that is provided.
SELECT CHOOSE ( 3, 'Manager', 'Director', 'Developer', 'Tester' ) AS Result;
Here is the result set.
Result ------------- Developer (1 row(s) affected)
The following example returns a simple character string based on the value in the ProductCategoryID column.
USE AdventureWorks2012; GO SELECT ProductCategoryID, CHOOSE (ProductCategoryID, 'A','B','C','D','E') AS Expression1 FROM Production.ProductCategory;
Here is the result set.
ProductCategoryID Expression1 ----------------- ----------- 3 C 1 A 2 B 4 D (4 row(s) affected)
The following example returns the quarter in which an employee was hired. The MONTH function is used to return the month value from the column HireDate.
USE AdventureWorks2012;
GO
SELECT JobTitle, HireDate, CHOOSE(MONTH(HireDate),'Winter','Winter', 'Spring','Spring','Spring','Summer','Summer',
'Summer','Autumn','Autumn','Autumn','Winter') AS Quarter_Hired
FROM HumanResources.Employee
WHERE YEAR(HireDate) > 2005
ORDER BY YEAR(HireDate);
Here is the result set.
JobTitle HireDate Quarter_Hired -------------------------------------------------- ---------- ------------- Sales Representative 2006-11-01 Autumn European Sales Manager 2006-05-18 Spring Sales Representative 2006-07-01 Summer Sales Representative 2006-07-01 Summer Sales Representative 2007-07-01 Summer Pacific Sales Manager 2007-04-15 Spring Sales Representative 2007-07-01 Summer