MySQL ELT() function
In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples.
Introduction of MySQL ELT() function
In the ELT function, the number field will state how many strings there will be.
ELT function in MySQL is used to return the string, which is at the index number specified in the argument list. In this function, there is a number field and a strings field.
Syntax of the MySQL ELT() function
The syntax of the MySQL ELT() function is given as follows:
ELT(I, string1, string2, string3, string4, …);
Parameters or arguments used in MySQL ELT() function:
There are two parameters accepted by the ELT() function in MySQL, which are given as follows:
I: It is an integer, and it is the index of the string to be retrieved.
string1, string2, string3: these are the List of strings from which we want to retrieve the index.
Returns:
It will return a string at the specified index. When there is no string at the specified index I, then it will return NULL.
Application used for ELT() function:
The ELT() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL ELT() function:
Now we will look into some MySQL ELT() function examples and will explore how we can use the ELT function in MySQL.
Example-1 :
Here, we will Retrieve a string using ELT() function in MySQL.
Select ELT(4, 'Learning', 'SQL', 'at', 'Javatpoint', 'is', 'fun') As Res_Str;
Output :
Res_Str
Javatpoint
1 row in set (0.00 sec)
Example-2 :
Here, we will Retrieve a string using ELT() function in MySQL.
Select ELT(1, 'Learning', 'SQL', 'at', 'Javatpoint', 'is', 'fun')
As Res_Str;
Output :
Res_Str
Learning
1 row in set (0.00 sec)
Example-3 :
Here, we will Retrieve a string using ELT() function when there is no string at the specified index.
Select ELT(8, 'Learning', 'SQL', 'at', 'Javatpoint', 'is', 'fun')
As Res_Str;
Output :
Res_Str
NULL
1 row in set (0.00 sec)
Example-4 :
Let's Consider a database table called Workerlogin with the following records:
WorkID | 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 the List of strings specified by column records:
SELECT *,
ELT(2, Name, Date, LoginTime) AS ELT_Value
FROM Workerlogin;
This will produce a result similar to the following:
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 |
Application of MySQL ELT() function:
This function is used to return the string, which is at the index number specified in the argument list.
Summary:
In the above context, we have learned how we can use the ELT() function in MySQL used to return the string, which is at the index number specified in the argument list.