MySQL FIND_IN_SET Function

Example

Search for “q” within the list of strings:

SELECT FIND_IN_SET("q", "s,q,l");

Definition and Usage

The FIND_IN_SET() function returns the position of a string within a list of strings.

Syntax

FIND_IN_SET(string, string_list)

Parameter Values

Parameter Description
string Required. The string to search for
string_list Required. The list of string values to be searched (separated by commas)

Return Values

  • If string is not found in string_list, this function returns 0
  • If string or string_list is NULL, this function returns NULL
  • If string_list is an empty string (“”), this function returns 0

Technical Details

Works in: From MySQL 4.0

More Examples

Example

Search for “a” within the list of strings:

SELECT FIND_IN_SET("a", "s,q,l");

Example

Search for “q” within the list of strings (string list is NULL):

SELECT FIND_IN_SET("q", null);

MySQL FIELD() Function

Example

Return the index position of  “q” in the string list:

SELECT FIELD("q", "s", "q", "l");

Definition and Usage

The FIELD() function returns the index position of a value in a list of values.

This function performs a case-insensitive search.

Note: If the specified value is not found in the list of values, this function will return 0. If value is NULL, this function will return 0.

Syntax

FIELD(value, val1, val2, val3, ...)

Parameter Values

Parameter Description
value Required. The value to search for in the list
val1, val2, val3, …. Required. The list of values to search

Technical Details

Works in: From MySQL 4.0

More Examples

Example

Return the index position of “c” in the string list:

SELECT FIELD("c", "a", "b");

Example

Return the index position of “Q” in the string list:

SELECT FIELD("Q", "s", "q", "l");

Example

Return the index position of 5 in the numeric list:

SELECT FIELD(5, 0, 1, 2, 3, 4, 5);

MySQL CONCAT_WS() Function

Example

Add several expressions together, and add a “-” separator between them:

SELECT CONCAT_WS("-", "SQL", "Tutorial", "is", "fun!") AS ConcatenatedString;

Definition and Usage

The CONCAT_WS() function adds two or more expressions together with a separator.

Note: Also look at the CONCAT() function. Continue reading MySQL CONCAT_WS() Function

MySQL CONCAT() Function

Example

Add several strings together:

SELECT CONCAT("SQL ", "Tutorial ", "is ", "fun!") AS ConcatenatedString;

Definition and Usage

The CONCAT() function adds two or more expressions together.

Note: Also look at the CONCAT_WS() function. Continue reading MySQL CONCAT() Function

MySQL CHARACTER_LENGTH() Function

Example

Return the length of the string:

SELECT CHARACTER_LENGTH("SQL Tutorial") AS LengthOfString;

Continue reading MySQL CHARACTER_LENGTH() Function

MySQL CHAR_LENGTH() Function

Example

Return the length of the string:

SELECT CHAR_LENGTH("SQL Tutorial") AS LengthOfString;

Continue reading MySQL CHAR_LENGTH() Function