Skip to main content

FIND_IN_SET

Description

Returns the position of the first occurrence of string str in the comma-separated string list strlist (counting starts from 1). This function is implemented in MySQL-compatible mode and is used to find specific strings in comma-separated value lists.

Syntax

FIND_IN_SET(<str>, <strlist>)

Parameters

ParameterDescription
<str>The target string to search for. Type: VARCHAR
<strlist>The comma-separated string list to search in for str. Type: VARCHAR

Return Value

Returns INT type, representing the position of str in strlist (counting starts from 1).

Search rules:

  • Exact match: Returns position only when str exactly matches a substring in strlist
  • Position counting starts from 1
  • Returns the position of the first match

Special cases:

  • If str is an empty string, returns 0
  • If strlist is an empty string, returns 0
  • If no match is found, returns 0
  • If any parameter is NULL, returns NULL
  • If str contains a comma, it cannot be matched correctly (because comma is the delimiter)
  • Matching is case-sensitive

Examples

  1. Basic search
SELECT FIND_IN_SET('b', 'a,b,c');
+---------------------------+
| FIND_IN_SET('b', 'a,b,c') |
+---------------------------+
| 2 |
+---------------------------+
  1. Find first element
SELECT FIND_IN_SET('apple', 'apple,banana,cherry');
+------------------------------------------+
| FIND_IN_SET('apple', 'apple,banana,cherry') |
+------------------------------------------+
| 1 |
+------------------------------------------+
  1. Find last element
SELECT FIND_IN_SET('cherry', 'apple,banana,cherry');
+-------------------------------------------+
| FIND_IN_SET('cherry', 'apple,banana,cherry') |
+-------------------------------------------+
| 3 |
+-------------------------------------------+
  1. No match found
SELECT FIND_IN_SET('orange', 'apple,banana,cherry');
+--------------------------------------------+
| FIND_IN_SET('orange', 'apple,banana,cherry') |
+--------------------------------------------+
| 0 |
+--------------------------------------------+
  1. NULL value handling
SELECT FIND_IN_SET(NULL, 'a,b,c'), FIND_IN_SET('b', NULL);
+---------------------------+-------------------------+
| FIND_IN_SET(NULL, 'a,b,c') | FIND_IN_SET('b', NULL) |
+---------------------------+-------------------------+
| NULL | NULL |
+---------------------------+-------------------------+
  1. Empty string handling
SELECT FIND_IN_SET('', 'a,b,c'), FIND_IN_SET('a', '');
+-------------------------+-----------------------+
| FIND_IN_SET('', 'a,b,c') | FIND_IN_SET('a', '') |
+-------------------------+-----------------------+
| 0 | 0 |
+-------------------------+-----------------------+
  1. String containing comma (cannot match correctly)
SELECT FIND_IN_SET('a,b', 'a,b,c,d');
+------------------------------+
| FIND_IN_SET('a,b', 'a,b,c,d') |
+------------------------------+
| 0 |
+------------------------------+
  1. Case-sensitive matching
SELECT FIND_IN_SET('B', 'a,b,c'), FIND_IN_SET('b', 'A,B,C');
+---------------------------+---------------------------+
| FIND_IN_SET('B', 'a,b,c') | FIND_IN_SET('b', 'A,B,C') |
+---------------------------+---------------------------+
| 0 | 0 |
+---------------------------+---------------------------+
  1. Partial match will not succeed
SELECT FIND_IN_SET('ap', 'apple,banana,cherry');
+---------------------------------------+
| FIND_IN_SET('ap', 'apple,banana,cherry') |
+---------------------------------------+
| 0 |
+---------------------------------------+
  1. Numeric string search
SELECT FIND_IN_SET('2', '1,2,3,10,20');
+--------------------------------+
| FIND_IN_SET('2', '1,2,3,10,20') |
+--------------------------------+
| 2 |
+--------------------------------+