Search for a substring within a string and return the position of the first occurrence. Returns 0 or NULL if not found, depending on the engine.
Returns the 1-based position of a substring within a string, or 0 if not found. Standard SQL equivalent of MySQL LOCATE and SQL Server CHARINDEX.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | all | INSTR(str, substr) and LOCATE(substr, str [, start]) are the native functions. POSITION(substr IN str) also works as SQL-standard syntax. Returns 0 if not found (not NULL). |
| PostgreSQL | ✓ Supported | all | POSITION(substr IN str) is the standard form. STRPOS(str, substr) is a PostgreSQL-specific alias with reversed argument order. Returns 0 if not found. No CHARINDEX or INSTR. |
| SQL Server | ✓ Supported | all | CHARINDEX(needle, haystack [, start]) is the primary function. Returns 0 if not found. PATINDEX('%pattern%', str) supports LIKE-style wildcards. No INSTR or POSITION. |
| Oracle | ✓ Supported | all | INSTR(str, substr [, start [, occurrence]]) is the primary function. Unique: supports nth-occurrence search and negative start position (search from end). Returns 0 if not found. |
| SQLite | ✓ Supported | all | INSTR(haystack, needle) returns the position of the first occurrence. Returns 0 if not found. Argument order is reversed compared to SQL Server's CHARINDEX. |
Argument order is a constant gotcha: MySQL's LOCATE and SQL Server's CHARINDEX take (needle, haystack), while PostgreSQL's STRPOS and SQLite's INSTR take (haystack, needle). Oracle's INSTR is the most powerful, supporting nth-occurrence and reverse search. All engines return 0 (not NULL) when the substring is not found.
INSTR(str, substr) and LOCATE(substr, str [, start]) are the native functions. POSITION(substr IN str) also works as SQL-standard syntax. Returns 0 if not found (not NULL).
POSITION(substr IN str) is the standard form. STRPOS(str, substr) is a PostgreSQL-specific alias with reversed argument order. Returns 0 if not found. No CHARINDEX or INSTR.
CHARINDEX(needle, haystack [, start]) is the primary function. Returns 0 if not found. PATINDEX('%pattern%', str) supports LIKE-style wildcards. No INSTR or POSITION.
INSTR(str, substr [, start [, occurrence]]) is the primary function. Unique: supports nth-occurrence search and negative start position (search from end). Returns 0 if not found.
INSTR(haystack, needle) returns the position of the first occurrence. Returns 0 if not found. Argument order is reversed compared to SQL Server's CHARINDEX.