ELT
Description
The ELT function returns the string at the specified index position. Index counting starts from 1.
Syntax
ELT(<pos>, <str>[, <str> ...])
Parameters
| Parameter | Description |
|---|---|
<pos> | Index position (starting from 1). Type: INT |
<str> | String list. Type: VARCHAR |
Return Value
Returns VARCHAR type, representing the string at the specified index position.
Special cases:
- If
<pos>is less than 1 or greater than the number of strings, returns NULL - If
<pos>is NULL, returns NULL - Index starts from 1, the first string has index 1
Examples
- Basic usage: Get the 1st string
SELECT ELT(1, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(1, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| aaa |
+-----------------------------+
- Get the 2nd string
SELECT ELT(2, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(2, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| bbb |
+-----------------------------+
- Index out of bounds returns NULL
SELECT ELT(0, 'aaa', 'bbb'), ELT(5, 'aaa', 'bbb');
+----------------------+----------------------+
| elt(0, 'aaa', 'bbb') | elt(5, 'aaa', 'bbb') |
+----------------------+----------------------+
| NULL | NULL |
+----------------------+----------------------+
- NULL value handling
SELECT ELT(NULL, 'aaa', 'bbb');
+-------------------------+
| elt(NULL, 'aaa', 'bbb') |
+-------------------------+
| NULL |
+-------------------------+
- Index exceeds range returns NULL
SELECT ELT(5, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(5, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| NULL |
+-----------------------------+
- Negative index returns NULL
SELECT ELT(-1, 'first', 'second');
+----------------------------+
| elt(-1, 'first', 'second') |
+----------------------------+
| NULL |
+----------------------------+
- UTF-8 string
SELECT
ELT(2, 'Hello', 'ṭṛ', 'Hola');
+-----------------------------------+
| ELT(2, 'Hello', 'ṭṛ', 'Hola') |
+-----------------------------------+
| ṭṛ |
+-----------------------------------+
- Handling empty strings
SELECT ELT(2, 'first', '', 'third');
+------------------------------+
| elt(2, 'first', '', 'third') |
+------------------------------+
| |
+------------------------------+