LOCATE()
This page is part of MariaDB's Documentation.
The parent of this page is: Functions for MariaDB Xpand
Topics on this page:
Overview
Returns the position of the first occurrence of a substring within a string.
USAGE
LOCATE(substring, string[, position])
Argument Name | Description |
---|---|
| The substring to locate |
| The string to search |
| The starting position for the search |
DETAILS
LOCATE()
is a string function that returns the position of the first occurrence of a substring a string.
The optional position
argument specifies a starting position for the search, which allows for the finding of subsequent string matches within the string after the first match.
A 0
is returned if the substring is not found within the string.
A NULL
is returned if any argument is NULL
.
EXAMPLES
SELECT LOCATE('abcd', 'abcacbabdcabceabcd');
+--------------------------------------+
| LOCATE('abcd', 'abcacbabdcabceabcd') |
+--------------------------------------+
| 15 |
+--------------------------------------+
SET @str = 'abcacbabdcabceabcd';
SELECT LOCATE('abc', @str) AS m1,
LOCATE('abc', @str, 2) AS m2,
LOCATE('abc', @str, 12) AS m3;
+------+------+------+
| m1 | m2 | m3 |
+------+------+------+
| 1 | 11 | 15 |
+------+------+------+