Skip to main content

LOCATE

Description​

Returns the position of substr in str (counting from 1). If the third parameter pos is specified, the position of substr is searched from the string starting with the pos subscript. If not found, 0 is returned

Syntax​

LOCATE ( <substr> , <str> [, <pos> ] )

Parameters​

ParameterDescription
substrThe substring to be searched
strThe string to be searched
posIf this parameter is specified, the position of substr is searched from the string starting with the pos subscript

Return value​

The position of substr in str (counting from 1)

Example​

SELECT LOCATE('bar', 'foobarbar'),LOCATE('xbar', 'foobar'),LOCATE('bar', 'foobarbar', 5)
+----------------------------+--------------------------+-------------------------------+
| locate('bar', 'foobarbar') | locate('xbar', 'foobar') | locate('bar', 'foobarbar', 5) |
+----------------------------+--------------------------+-------------------------------+
| 4 | 0 | 7 |
+----------------------------+--------------------------+-------------------------------+