Function Reference

LOCATE

PostgreSQL equivalent:

Returns the position of the first occurrence of a substring in a string.

MySQL 8.1
LOCATE(substr, str[, start_position])

Parameters

substrVARCHAR
Substring to find
strVARCHAR
String to search in
start_positionINToptional
Position to start the search from, 1 by default

Return value

An integer position starting from 1; 0 if the substring is not found

Examples

Position of a word in a string

MySQL 8.1
SELECT LOCATE('world', 'Hello world');
LOCATE('world', 'Hello world')
7

Search from the third position

MySQL 8.1
SELECT LOCATE('a', 'banana', 3);
LOCATE('a', 'banana', 3)
4

Details

The position is always counted from the beginning of the string, even if start_position is given. The search is case-insensitive by default.

See also