MariaDB FIND_IN_SET() Function

In MariaDB, the FIND_IN_SET() function returns the index position where the given value occurs in the specified string list.

The FIND_IN_SET() function is similar to the FIELD() function, except that the list in FIELD() is composed of multiple parameters.

MariaDB FIND_IN_SET() Syntax

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

FIND_IN_SET(value, string_list)

Parameter Description

value

Required. This is the string to search for.

string_list

Required. This is a comma-separated list of strings.

If you provide no parameters or 1 parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'FIND_IN_SET'.

Return value

If string_list includes value, the FIND_IN_SET() function returns the position index of the first match.

If string_list does not includes value, the FIND_IN_SET() function will return 0.

If value is NULL, the FIND_IN_SET() function will return NULL.

If string_list is an empty string, the FIND_IN_SET() function will return 0.

If string_list is NULL, then the FIND_IN_SET() function will return NULL.

MariaDB FIND_IN_SET() Examples

Basic example

If you need to know the position of 'A' in the list A,B,C, you can use the following statement:

SELECT FIND_IN_SET('A', 'A,B,C');

Output:

+---------------------------+
| FIND_IN_SET('A', 'A,B,C') |
+---------------------------+
|                         1 |
+---------------------------+

Find Numbers

You can also find a number from a number list, as follows:

SELECT FIND_IN_SET(1, '3,2,1');

Output:

+-------------------------+
| FIND_IN_SET(1, '3,2,1') |
+-------------------------+
|                       3 |
+-------------------------+

Case insensitive

SELECT FIND_IN_SET('a', 'A,B,C');

Output:

+---------------------------+
| FIND_IN_SET('a', 'A,B,C') |
+---------------------------+
|                         1 |
+---------------------------+

Not Founded

If there is no match, the FIND_IN_SET() function will return 0.

SELECT FIND_IN_SET('D', 'A,B,C');

Output:

+---------------------------+
| FIND_IN_SET('D', 'A,B,C') |
+---------------------------+
|                         0 |
+---------------------------+

Conclusion

In MariaDB, the FIND_IN_SET() function returns the index position of the first occurrence of the given value in the specified string list.