LOCATE
PostgreSQL equivalent:
Returns the position of the first occurrence of a substring in a string.
MySQL 8.1
LOCATE(substr, str[, start_position])- [ ]optional part
- substr
- Substring to findVARCHAR
- str
- String to search inVARCHAR
- start_position
- Position to start the search from, 1 by defaultINTOptional
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');Search from the third position
MySQL 8.1
SELECT LOCATE('a', 'banana', 3);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.