THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL POSITION() Function

❮ MySQL Functions

Example

Search for "3" in string "W3Schools.com", and return position:

SELECT POSITION("3" IN "W3Schools.com") AS MatchPosition;
Try it Yourself »

Definition and Usage

The POSITION() function returns the position of the first occurrence of a substring in a string.

Note: The LOCATE() function is a synonym for the POSITION() function.

Syntax

POSITION(substring IN string)

Parameter Values

Parameter Description
substring Required. The substring to search for in string
string Required. The string that will be searched

Note

  • POSITION() performs a case-insensitive search
  • The first position in string is 1
  • If substring is not found within string, the POSITION() function will return 0

Technical Details

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

More Examples

Example

Search for "COM" in string "W3Schools.com", and return position:

SELECT POSITION("COM" IN "W3Schools.com") AS MatchPosition;
Try it Yourself »

Example

Search for "a" in CustomerName column, and return position:

SELECT POSITION("a" IN CustomerName)
FROM Customers;
Try it Yourself »

❮ MySQL Functions