Menu

MySQL INSTR() Function: Find a Substring Position

Updated on

MySQL INSTR(str, substr) returns the position of the first occurrence of substr in str. The result is 1-based; it is 0 when no match is found and NULL if either argument is NULL. See the MySQL string function reference.

Syntax

INSTR(str, substr)

The argument order is the string first and the substring second:

SELECT
    INSTR('foobarbar', 'bar') AS found_at,
    INSTR('foobar', 'baz') AS not_found,
    INSTR('foobar', NULL) AS null_argument;
found_at  not_found  null_argument
--------  ---------  -------------
4         0          NULL

INSTR() is the two-argument form of LOCATE(substr, str), with the arguments reversed. Use LOCATE() when you also need its optional starting-position argument. POSITION(substr IN str) is another spelling of the same two-argument search.

For nonbinary strings, matching follows the arguments’ collation. MySQL treats the search as case-sensitive if either argument is a binary string.