ELT() FUNCTION in MySQL

ELT() FUNCTION
The MySQL ELT function is used to extract the nth string from the list of strings.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ELT( n, string_1, string_2, … string_n );
ELT( n, string_1, string_2, … string_n );
ELT( n, string_1, string_2, … string_n );

Parameters:
n: It is used to specify the index of the string to extract.
string_1, string_2, … string_n: It is used to specify a list of strings.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT ELT ( 2, ‘a’, ‘b’, ‘c’, ‘d’ );
mysql> SELECT ELT ( 2, ‘a’, ‘b’, ‘c’, ‘d’ );
mysql> SELECT ELT ( 2, ‘a’, ‘b’, ‘c’, ‘d’ );

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
‘b’
‘b’
‘b’

Explanation:
The 2nd element is extracted and returned from the list of strings.