SQL CHOOSE function
CHOOSE(2, 'low', 'medium', 'high') returns 'medium'. It is shorthand for a CASE on a position number.
- Categoría
- Condicional
- Returns
the highest-precedence type among the values- In this category
- 38 of 43
Sintaxis
CHOOSE(index, value_1, value_2 [, ...])
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sí | Supported under this name. |
| PostgreSQL | Different name or syntax | (ARRAY[a, b, c])[i] |
| MySQL | Different name or syntax | ELT(i, a, b, c), which returns strings |
| SQLite | Not built in | no function; CASE i WHEN 1 THEN a WHEN 2 THEN b END |
| Oracle | Different name or syntax | DECODE(i, 1, a, 2, b, 3, c) |
Ejemplos
SQL Server
SELECT CHOOSE(2, 'low', 'medium', 'high') AS level, CHOOSE(5, 'a', 'b') AS out_of_range; -- medium, NULL
SQL Server
SELECT CHOOSE(DATEPART(QUARTER, '2026-08-15'), 'Q1', 'Q2', 'Q3', 'Q4') AS quarter_label; -- Q3
PostgreSQL
SELECT (ARRAY['low', 'medium', 'high'])[2] AS level; -- medium
MySQL
SELECT ELT(2, 'low', 'medium', 'high') AS level; -- medium
Conviene saberlo
- The index is 1-based and truncated to an integer: CHOOSE(1.9, 'x', 'y') returns 'x'.
- An index out of range returns NULL, not an error: CHOOSE(5, 'a', 'b') is NULL.
- The result takes the highest-precedence type of the values, as CASE does. CHOOSE(1, 'a', 2) fails converting 'a' to int even though 2 is never chosen.
Questions about CHOOSE
CHOOSE or IIF?
IIF picks between two values on a condition; CHOOSE picks among many by position. Both expand to CASE internally.
Is CHOOSE zero-based?
No. The first value is index 1, and CHOOSE(0, ...) returns NULL.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.