Mysql Find_in_set Function

MySQL: FIND_IN_SET Function

This tutorial explains how to use the MySQL FIND_IN_SET function with syntax and examples.

Description

The MySQL FIND_IN_SET function returns the position of a string in a comma-delimited string list.

Syntax

The syntax for the FIND_IN_SET function in MySQL is:

FIND_IN_SET( string, string_list )

Parameters or Arguments

string

The string to find.

string_list

The list of string values separated by commas that is to be searched.

Note

  • If string is not found in string_list, the FIND_IN_SET function will return 0.
  • If string 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, the FIND_IN_SET function will return NULL.

Applies To

The FIND_IN_SET function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let's look at some MySQL FIND_IN_SET function examples and explore how to use the FIND_IN_SET function in MySQL.

For example:

mysql> SELECT FIND_IN_SET('b', 'a,b,c,d,e,f');
Output: 2

mysql> SELECT FIND_IN_SET('B', 'a,b,c,d,e,f');
Output: 2

mysql> SELECT FIND_IN_SET('f', 'a,b,c,d,e,f');
Output: 6

mysql> SELECT FIND_IN_SET(2,'1,2,3');
Output: 2

mysql> SELECT FIND_IN_SET('g', 'a,b,c,d,e,f');
Output: 0

mysql> SELECT FIND_IN_SET('g', '');
Output: 0

mysql> SELECT FIND_IN_SET(null, 'a,b,c');
Output: NULL

mysql> SELECT FIND_IN_SET('a', null);
Output: NULL