MariaDB ELT() Function

In MariaDB, ELT() is a built-in string function that searches for the parameter at the position specified by the first parameter starting from the second parameter and returns it as a string.

MariaDB ELT() Syntax

Here is the syntax of the MariaDB ELT() function:

ELT(pos, str1[, str2, ...])

Parameters

pos

Required. A number specifying the position. If you provide a floating point number, MariaDB will automatically round it to an integer.

str1[, str2, ...]

Required. List of string parameters. You should specify at least one parameter. If you provide no parameters, or only one parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'ELT'.

Return value

The MariaDB ELT(pos, str1, str2, ...) function returns the parameter value at the specified position.

If pos is 1, return str1; if pos is 2; return str2, and so on.

Returns NULL if pos is less than 1 or exceeds the number of arguments.

The MariaDB ELT() function will return NULL if pos cannot be converted to a number.

MariaDB ELT() Examples

Basic example

Here’s a basic example:

SELECT
  ELT(1, 'Apple', 'Banana'),
  ELT(2, 'Apple', 'Banana');

Output:

+---------------------------+---------------------------+
| ELT(1, 'Apple', 'Banana') | ELT(2, 'Apple', 'Banana') |
+---------------------------+---------------------------+
| Apple                     | Banana                    |
+---------------------------+---------------------------+

In this example, ELT(1, 'Apple', 'Banana') returned the 1st string parameter Apple and ELT(2, 'Apple', 'Banana') returned the 2nd string parameter Banana.

Too large or too small position

The MariaDB ELT() function will return NULL if the first argument is less than 1 or exceeds the number of string arguments. The following example illustrates this:

SELECT
  ELT(0, 'Apple', 'Banana') "0",
  ELT(-1, 'Apple', 'Banana') "-1",
  ELT(3, 'Apple', 'Banana') "3";

Output:

+------+------+------+
| 0    | -1   | 3    |
+------+------+------+
| NULL | NULL | NULL |
+------+------+------+

Float parameters

The following example illustrates that the MariaDB ELT() function rounds float parameters to integers:

SELECT
  ELT(1.4, 'Apple', 'Banana'),
  ELT(1.6, 'Apple', 'Banana');

Output:

+-----------------------------+-----------------------------+
| ELT(1.4, 'Apple', 'Banana') | ELT(1.6, 'Apple', 'Banana') |
+-----------------------------+-----------------------------+
| Apple                       | Banana                      |
+-----------------------------+-----------------------------+

Conclusion

The first parameter of the MariaDB ELT() function is a number, and the other parameters are strings, and it returns the string parameter at the position specified by the first parameter.