How to Pick a String by Position with MariaDB ELT()
MariaDB’s ELT(N, str1, str2, ...) returns the Nth string in its argument list. Positions start at 1, so ELT(2, 'A', 'B', 'C', 'D') returns B. A position outside the list, or a position of NULL, returns NULL.
Check the position and return value
ELT() takes a position followed by at least one string. The first string after the position is item 1. The table varies the position for the same four items.
| Position N | Return value |
|---|---|
1 |
A |
2 |
B |
4 |
D |
0 |
NULL |
-1 |
NULL |
5 |
NULL |
NULL |
NULL |
SELECT ELT(1, 'A', 'B', 'C', 'D') AS first_item,
ELT(2, 'A', 'B', 'C', 'D') AS second_item,
ELT(4, 'A', 'B', 'C', 'D') AS last_item,
ELT(0, 'A', 'B', 'C', 'D') AS zero_position,
ELT(-1, 'A', 'B', 'C', 'D') AS negative_position,
ELT(5, 'A', 'B', 'C', 'D') AS past_end,
ELT(NULL, 'A', 'B') AS missing_position,
ELT(2, 'A', NULL) AS missing_item;
When a valid position points to an item that is NULL, the result is also NULL. Check the position and source item separately if you need to distinguish a missing value from an invalid position.
See how decimal positions become integers
Pass an integer to make the intended position clear. With the decimal literals 1.4 and 1.5 used below, MariaDB selects positions 1 and 2, returning A and B.
SELECT ELT(1.4, 'A', 'B', 'C', 'D') AS from_1_4,
ELT(1.5, 'A', 'B', 'C', 'D') AS from_1_5,
ELT(ROUND(1.5, 0), 'A', 'B', 'C', 'D') AS rounded_first,
ELT(TRUNCATE(1.9, 0), 'A', 'B', 'C', 'D') AS truncated_first;
Use ROUND() first if the position should be rounded, or TRUNCATE() if the rule discards the fractional part. In the example, from_1_5 and rounded_first are B, while truncated_first is A.
Map consecutive status codes to labels
When status codes are consecutive and their order is fixed, ELT() can assign labels directly. Wrap it in COALESCE() to provide a fallback for display.
SELECT id, status_code,
ELT(status_code, 'Pending', 'Working', 'Done') AS status_label,
COALESCE(ELT(status_code, 'Pending', 'Working', 'Done'),
'Unknown') AS display_label
FROM (
SELECT 1 AS id, 1 AS status_code
UNION ALL SELECT 2, 2
UNION ALL SELECT 3, 3
UNION ALL SELECT 4, 9
UNION ALL SELECT 5, NULL
) AS sample_status
ORDER BY id;
Codes 1, 2, and 3 map to Pending, Working, and Done. Rows with code 9 or NULL retain NULL in status_label and show Unknown in display_label. Keep the original code visible when you need to identify unmapped rows.
Choose between ELT, FIELD, and CASE
Use FIELD() to look up a position from a string. For codes such as 10, 20, and 30, use CASE to match each code directly.
SELECT FIELD('Working', 'Pending', 'Working', 'Done') AS found_position,
FIELD('Cancelled', 'Pending', 'Working', 'Done') AS missing_position,
CASE 20
WHEN 10 THEN 'Pending'
WHEN 20 THEN 'Working'
WHEN 30 THEN 'Done'
ELSE 'Unknown'
END AS sparse_label;
Working has position 2. Cancelled is absent, so FIELD() returns 0. The CASE expression maps code 20 to Working. A lookup table joined to the source data is easier to maintain when several queries share a mapping or its labels change often.



