CHOOSE (TSQL Function)
Returns the value corresponding to the specified index in the list of values.
Syntax
CHOOSE ( index , value_1 , value_2 [, ...value_n] )
Arguments
- index
- is an expression of type
int
, 1-based. - value_1 … value_n
- is a list of expressions of any type.
Return Types
Returns the type which has the highest precedence from the list of value_1 to value_n.
Remarks
If index is larger than the number of values in the list, NULL is returned.
Examples
PRINT CHOOSE(3, 'Paris', 'Geneva', 'Moscow', 'Berlin');
PRINT CHOOSE(5, 'Paris', 'Geneva', 'Moscow', 'Berlin');
The result is:
Moscow
<NULL>
Example with different argument types:
PRINT CHOOSE(1, '10000', 'Geneva', 123, 'Berlin'); -- the string '2000' can be converted to int
PRINT CHOOSE(1, 'Paris', 'Geneva', 123, 'Berlin'); -- the string 'Paris' cannot be converted to int
The result type is int, because it has higher precedence than varchar:
10000
<Conversion error. VARCHAR "Paris" cannot be cast into INT, invalid string.>