Outdated Version

You are viewing an older version of this section. View current production version.

LOCATE

Returns the first position of the given search string.

Syntax

LOCATE (needle, haystack)
LOCATE (needle, haystack, startpos)

Arguments

  • needle: string to search for
  • haystack: any string or binary object
  • startpos: character position to start searching at
Info

This function is identical to INSTR but with the arguments reversed.

Return Type

Integer position, one-indexed. 0 if not found.

Examples

+---------------------+
| locate('i', 'ohai') |
+---------------------+
|                   4 |
+---------------------+

memsql> select locate('z', 'ohai');
+---------------------+
| locate('z', 'ohai') |
+---------------------+
|                   0 |
+---------------------+

memsql> select locate('i', 'ohaiohai', 6);
+----------------------------+
| locate('i', 'ohaiohai', 6) |
+----------------------------+
|                          8 |
+----------------------------+