Elt Function In Sql in SQL
? ELT() Function in SQL
The ELT() function in SQL returns the N-th string from a list of strings, based on the index number provided.
It is commonly used in MySQL.
? Syntax:
ELT(index, str1, str2, str3, ..., strN)index: An integer that indicates which string to return.
str1, str2, ...: List of strings to choose from.
? Example:
SELECT ELT(2, 'Apple', 'Banana', 'Cherry');? Output:
BananaWhy? Because 2 means return the second item in the list.
? Edge Cases:
| Example | Output | Explanation |
|---|---|---|
ELT(1, 'A', 'B', 'C') | 'A' | Returns 1st element |
ELT(3, 'X', 'Y', 'Z') | 'Z' | Returns 3rd element |
ELT(4, 'P', 'Q', 'R') | NULL | No 4th element exists |
ELT(0, 'One', 'Two') | NULL | Index must be ? 1 |
? Use Case Example (with a Table):
SELECT name, ELT(gender, 'Male', 'Female') AS gender_textFROM employees;If gender is stored as 1 for Male and 2 for Female, this will convert it into readable text.
Let me know if you want to compare ELT() with FIELD() or CASE.