string

POSITION / INSTR

Returns the 1-based index of the first occurrence of substr in string, or 0 if not found. POSITION is SQL-standard (Postgres/MySQL); INSTR is the MySQL/SQLite shorthand.

POSITION(substr IN string)  |  INSTR(string, substr)

Parameters / Modifiers

Parameter Purpose
STRPOS Postgres alias for POSITION with reversed argument order
LOCATE(sub, str, start) MySQL variant that accepts a starting offset
NULLIF(..., 0) Common wrapper to convert 'not found' 0 into NULL

Examples

SELECT POSITION('@' IN '[email protected]');

Returns 4

SELECT INSTR('[email protected]', '@');

Same result, MySQL/SQLite/Oracle syntax

SELECT STRPOS('hello world', 'world');

Postgres helper — returns 7

SELECT LOCATE('l', 'hello world', 5);

MySQL: search starting at position 5, returns 10

Dialect notes / Gotcha

Returns 0 (not NULL) when the substring is missing — combine with NULLIF(POSITION(...), 0) if you want NULL. INSTR argument order is reversed vs POSITION (string first, then needle).

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet