LOCATE() FUNCTION in MySQL

LOCATE() FUNCTION
The MySQL LOCATE function is used to get the location of the first appearance of a substring in a string. The various versions of MySQL supports the LOCATE function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax:

LOCATE( substring, string, start_position )

Parameters:
substring: It is used to specify the substring to search for.
string: It is used to specify the string to search.
start_position: It is an optional parameter which is used to specify the position to start the search from.

Example 1:

mysql> SELECT LOCATE ( ‘L’ 'HELLO WORLD' );

Output:

3

Explanation:
The position of the first appearance of the substring starting from the first position is returned as the result.

Example 2:

mysql> SELECT LOCATE ( ‘l’ 'HELLO WORLD', 5 );

Output:

10

Explanation:
The position of the first appearance of the substring starting from the 5th position is returned as the result.

Please Share