SQL function
instr(), strpos() and POSITION(): find text in a string
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
SQLite's instr(text, part) returns the position of the first occurrence of part, counting from 1, or 0 if it isn't there. PostgreSQL spells it strpos(text, part) or the standard POSITION(part IN text), and has no instr.
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | strpos and POSITION: yes. instr: no | instr: yes. strpos, POSITION: no |
| Syntax | strpos(text, part) / POSITION(part IN text) | instr(text, part) |
Examples
Where the @ is in each email
PostgreSQL
SELECT LOWER(TRIM(email)) AS email, strpos(LOWER(TRIM(email)), '@') AS at_position
FROM adoption_requests
ORDER BY id
LIMIT 5;SQLite
SELECT LOWER(TRIM(email)) AS email, instr(LOWER(TRIM(email)), '@') AS at_position
FROM adoption_requests
ORDER BY id
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| at_position | |
|---|---|
| ana@mail.com | 4 |
| ben@mail.com | 4 |
| ana@mail.com | 4 |
| cara@mail.com | 5 |
| dev@mail.com | 4 |
Try it: PostgreSQL playground · SQLite playground
Names that contain a space
Greater than 0 means found, a handy alternative to LIKE '% %'.
PostgreSQL
SELECT name FROM menu_items
WHERE strpos(name, ' ') > 0
ORDER BY name;SQLite
SELECT name FROM menu_items
WHERE instr(name, ' ') > 0
ORDER BY name;Result (PostgreSQL and SQLite, identical): 7 rows
| name |
|---|
| BBQ Meteor |
| Caesar Salad |
| Four Cheese Orbit |
| Garlic Knots |
| Lemon Soda |
| Pepperoni Rocket |
| Veggie Nebula |
Try it: PostgreSQL playground · SQLite playground
With substr: the domain after the @
PostgreSQL
SELECT DISTINCT substr(LOWER(TRIM(email)), strpos(LOWER(TRIM(email)), '@') + 1) AS domain
FROM adoption_requests;SQLite
SELECT DISTINCT substr(LOWER(TRIM(email)), instr(LOWER(TRIM(email)), '@') + 1) AS domain
FROM adoption_requests;Result (PostgreSQL and SQLite, identical): 1 row
| domain |
|---|
| mail.com |
Try it: PostgreSQL playground · SQLite playground
How it behaves: edge cases, measured
Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Found | strpos('Biscuit', 'cui')4 | instr('Biscuit', 'cui')4 |
| Not found | strpos('Biscuit', 'x')0 | instr('Biscuit', 'x')0 |
| The first of several | strpos('banana', 'an')2 | instr('banana', 'an')2 |
| Case matters | strpos('Biscuit', 'b')0 | instr('Biscuit', 'b')0 |
| The standard POSITION syntax | POSITION('cui' IN 'Biscuit')4 | POSITION('cui' IN 'Biscuit')error: no such table: Biscuit |
| An empty search text | strpos('Biscuit', '')1 | instr('Biscuit', '')1 |
| NULL | strpos(NULL, 'a')NULL | instr(NULL, 'a')NULL |
Related
Common mistakes
Using instr in PostgreSQL or strpos in SQLite: each has only its own name (POSITION works in PostgreSQL only, as the matrix shows).
Forgetting that 0 means “not found”:
substr(x, instr(x, '@') + 1)returns the whole string when there's no @.Expecting a case-insensitive search: wrap both sides in LOWER().
PostgreSQL and SQLite
Same idea, different names: instr in SQLite, strpos and POSITION in PostgreSQL. Both return 1-based positions and 0 when nothing is found.
Official documentation
Go deeper
- Guide: SQL string functions
- Practice: SQL text practice: LIKE patterns and string functions
- Lesson: Text functions (Pro)
Make it stick in five minutes a day
“Text functions” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.