This function is used to return the position of substr in str. You can specify the starting position of your search using "start_pos," which starts from 1.
locate(string <substr>, string <str>[, bigint <start_pos>])
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
str |
Yes |
STRING |
Target string to be searched for If the value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, the value is implicitly converted to the STRING type for calculation. For other types of values, an error is reported. |
substr |
Yes |
STRING |
Substring to be matched If the value is of the BIGINT, DOUBLE, DECIMAL, or DATETIME type, the value is implicitly converted to the STRING type for calculation. For other types of values, an error is reported. |
start_pos |
No |
BIGINT |
Start position for the search |
The return value is of the BIGINT type.
The value 1 is returned.
select locate('ab', 'abhiab');
The value 5 is returned.
select locate('ab', 'abhiab', 2);
The value 0 is returned.
select locate('ab', 'abhiab', null);
The value 0 is returned.
select locate('hi', 'hanmeimei and lilei');