LOCATE() in MySQL — How It Works

What the MySQL LOCATE() function does, with an example.

Published October 19, 2026

LOCATE(substr, str[, pos]) returns the 1-based position of the first occurrence of substr in str, or 0 if not found.

How it works

INSTR(str, substr) is the same with swapped arguments.

Try it in MySQL

SELECT LOCATE('c', 'abcabc')
3
SELECT LOCATE('c', 'abcabc', 4)
6
SELECT LOCATE('z', 'abc')
0

Practical examples

Example 1

SELECT LOCATE('c', 'abcabc');
-- 3

Example 2

SELECT LOCATE('c', 'abcabc', 4);
-- 6

Example 3

SELECT LOCATE('z', 'abc');
-- 0

Use it in a real query

SELECT id, LOCATE('c', 'abcabc') AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Mixing argument order with INSTR.
  • Treating 0 as 'position zero' (it means not found).
  • Using LOCATE for indexed searches instead of LIKE 'x%'.

Key takeaways

  • LOCATE() in one line: LOCATE(substr, str[, pos]) returns the 1-based position of the first occurrence of substr in str, or 0 if not found.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

LOCATE vs INSTR?

Same result, opposite argument order.

Does LOCATE() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles