Skip to main content

ELT

Description

The ELT function returns the string at the specified index position. Index counting starts from 1.

Syntax

ELT(<pos>, <str>[, <str> ...])

Parameters

ParameterDescription
<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

  1. Basic usage: Get the 1st string
SELECT ELT(1, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(1, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| aaa |
+-----------------------------+
  1. Get the 2nd string
SELECT ELT(2, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(2, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| bbb |
+-----------------------------+
  1. 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 |
+----------------------+----------------------+
  1. NULL value handling
SELECT ELT(NULL, 'aaa', 'bbb');
+-------------------------+
| elt(NULL, 'aaa', 'bbb') |
+-------------------------+
| NULL |
+-------------------------+
  1. Index exceeds range returns NULL
SELECT ELT(5, 'aaa', 'bbb', 'ccc');
+-----------------------------+
| elt(5, 'aaa', 'bbb', 'ccc') |
+-----------------------------+
| NULL |
+-----------------------------+
  1. Negative index returns NULL
SELECT ELT(-1, 'first', 'second');
+----------------------------+
| elt(-1, 'first', 'second') |
+----------------------------+
| NULL |
+----------------------------+
  1. UTF-8 string
SELECT 
ELT(2, 'Hello', 'ṭṛ', 'Hola');
+-----------------------------------+
| ELT(2, 'Hello', 'ṭṛ', 'Hola') |
+-----------------------------------+
| ṭṛ |
+-----------------------------------+
  1. Handling empty strings
SELECT ELT(2, 'first', '', 'third');
+------------------------------+
| elt(2, 'first', '', 'third') |
+------------------------------+
| |
+------------------------------+