MySQL ELT() Function
The MySQL ELT() function returns the Nth element of the list of strings: str1 if N = 1, str2 if N = 2, and so on. It returns NULL if N is less than 1 or greater than the number of the string specified as arguments. This function is the complement of FIELD() function.
Syntax
ELT(N, str1, str2, str3,...)
Parameters
N |
Required. Specify the index number. |
str1, str2, str3,... |
Required. Specify the list of strings. |
Return Value
Returns the Nth element of the list of strings.
Example 1:
The example below shows the usage of ELT() function.
mysql> SELECT ELT(1, 'Learning', 'MySQL', 'is', 'fun'); Result: 'Learning' mysql> SELECT ELT(2, 'Learning', 'MySQL', 'is', 'fun'); Result: 'MySQL' mysql> SELECT ELT(5, 'Learning', 'MySQL', 'is', 'fun'); Result: NULL mysql> SELECT ELT(0, 'Learning', 'MySQL', 'is', 'fun'); Result: NULL mysql> SELECT ELT(4, 'Learning', 'MySQL', 'is', 'fun'); Result: 'fun' mysql> SELECT ELT(3, 10, 20, 30, '40'); Result: 30
Example 2:
Consider a database table called EmployeeLogin with the following records:
EmpID | Name | Date | LoginTime |
---|---|---|---|
1 | John | 2019-10-25 | 09:20:38 |
2 | Marry | 2019-10-25 | 09:21:05 |
3 | Jo | 2019-10-25 | 09:24:35 |
4 | Kim | 2019-10-25 | 09:25:24 |
5 | Ramesh | 2019-10-25 | 09:27:16 |
The following query can be used to get the 2nd element from list of strings specified by column records:
SELECT *, ELT(2, Name, Date, LoginTime) AS ELT_Value FROM EmployeeLogin;
This will produce a result similar to:
EmpID | Name | Date | LoginTime | ELT_Value |
---|---|---|---|---|
1 | John | 2019-10-25 | 09:20:38 | 2019-10-25 |
2 | Marry | 2019-10-25 | 09:21:05 | 2019-10-25 |
3 | Jo | 2019-10-25 | 09:24:35 | 2019-10-25 |
4 | Kim | 2019-10-25 | 09:25:24 | 2019-10-25 |
5 | Ramesh | 2019-10-25 | 09:27:16 | 2019-10-25 |
6 | Suresh | 2019-10-25 | 09:28:19 | 2019-10-25 |
❮ MySQL Functions