sqlstreak

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

PostgreSQLSQLite
Availablestrpos and POSITION: yes. instr: noinstr: yes. strpos, POSITION: no
Syntaxstrpos(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

emailat_position
ana@mail.com4
ben@mail.com4
ana@mail.com4
cara@mail.com5
dev@mail.com4

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.

CasePostgreSQLSQLite
Foundstrpos('Biscuit', 'cui')4instr('Biscuit', 'cui')4
Not foundstrpos('Biscuit', 'x')0instr('Biscuit', 'x')0
The first of severalstrpos('banana', 'an')2instr('banana', 'an')2
Case mattersstrpos('Biscuit', 'b')0instr('Biscuit', 'b')0
The standard POSITION syntaxPOSITION('cui' IN 'Biscuit')4POSITION('cui' IN 'Biscuit')error: no such table: Biscuit
An empty search textstrpos('Biscuit', '')1instr('Biscuit', '')1
NULLstrpos(NULL, 'a')NULLinstr(NULL, 'a')NULL

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

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.

Start free