The CHOOSE
function returns a value from a list of values.
The value chosen is specified by an index.
The index starts at 1, not 0.
This example return the third value from a list of values.
SELECT CHOOSE(3, 'HTML', 'CSS', 'SQL', 'JavaScript') AS Chosen
Chosen |
---|
SQL |
Syntax of the CHOOSE function.
CHOOSE(index, value1, value2, ..., valuen)
index
-- an integer that represents the index (i.e. position).
values
-- a comma separated list of values to look up.
ORDER |
---|
Id |
OrderDate |
OrderNumber |
CustomerId |
TotalAmount |
SELECT CHOOSE(DATEPART(quarter, OrderDate),
'Early', 'Mid', 'Late', 'Last') AS 'Quarter',
SUM(TotalAmount) AS 'Total Sales'
FROM [Order]
WHERE YEAR(OrderDate) = 2013
GROUP BY DATEPART(quarter, OrderDate)
Quarter | Total Sales |
---|---|
Early | 147879.90 |
Mid | 151611.09 |
Late | 165179.64 |
Last | 193718.12 |
Note: The list of values in the SQL query effectively represents a lookup table.