SQL how-to
How to split a string in SQL (PostgreSQL and SQLite)
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
PostgreSQL: split_part(email, '@', 2) returns the second piece. SQLite has no split function, so find the separator with instr and cut with substr: substr(email, instr(email, '@') + 1). The instr/substr form works in PostgreSQL too, spelt strpos.
The task: Split text at a separator, such as an email into its name and domain, or a list into rows.
Email name and domain
PostgreSQL
SELECT LOWER(TRIM(email)) AS email,
split_part(LOWER(TRIM(email)), '@', 1) AS name,
split_part(LOWER(TRIM(email)), '@', 2) AS domain
FROM adoption_requests
ORDER BY id
LIMIT 5;SQLite
SELECT LOWER(TRIM(email)) AS email,
substr(LOWER(TRIM(email)), 1, instr(LOWER(TRIM(email)), '@') - 1) AS name,
substr(LOWER(TRIM(email)), instr(LOWER(TRIM(email)), '@') + 1) AS domain
FROM adoption_requests
ORDER BY id
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| name | domain | |
|---|---|---|
| ana@mail.com | ana | mail.com |
| ben@mail.com | ben | mail.com |
| ana@mail.com | ana | mail.com |
| cara@mail.com | cara | mail.com |
| dev@mail.com | dev | mail.com |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
split_part(x, '@', 2)PostgreSQL: the second piece of x when cut at every @.
- 2
instr(x, '@')SQLite: the position of the first @ (strpos in PostgreSQL).
- 3
substr(x, position + 1)Everything after it.
More examples
First and last name
Split at the first space. Names with more than two parts need more care.
PostgreSQL
SELECT name,
split_part(name, ' ', 1) AS first_name,
substr(name, strpos(name, ' ') + 1) AS last_name
FROM staff
ORDER BY id;SQLite
SELECT name,
substr(name, 1, instr(name, ' ') - 1) AS first_name,
substr(name, instr(name, ' ') + 1) AS last_name
FROM staff
ORDER BY id;Result (PostgreSQL and SQLite, identical): 8 rows
| name | first_name | last_name |
|---|---|---|
| Rosa Vega | Rosa | Vega |
| Omar Haddad | Omar | Haddad |
| Lia Moreau | Lia | Moreau |
| Ben Carter | Ben | Carter |
| Yuki Tanaka | Yuki | Tanaka |
| Ivan Petrov | Ivan | Petrov |
| Zara Ali | Zara | Ali |
| Noah Kim | Noah | Kim |
Try it: PostgreSQL playground · SQLite playground
A list into rows
PostgreSQL unnests an array; SQLite walks the string with a recursive CTE.
PostgreSQL
SELECT unnest(string_to_array('mobile,desktop,tv', ',')) AS device;SQLite
WITH RECURSIVE parts(device, rest) AS (
SELECT '', 'mobile,desktop,tv' || ','
UNION ALL
SELECT substr(rest, 1, instr(rest, ',') - 1), substr(rest, instr(rest, ',') + 1)
FROM parts WHERE rest <> ''
)
SELECT device FROM parts WHERE device <> '';Result (PostgreSQL and SQLite, identical): 3 rows
| device |
|---|
| mobile |
| desktop |
| tv |
Try it: PostgreSQL playground · SQLite playground
Variations
The last piece
PostgreSQL 14+ accepts a negative position: split_part(x, '.', -1) gives the last part.
Common mistakes
No separator in the value: instr returns 0 and substr gives the wrong slice. Check with
CASE WHEN instr(x, '@') > 0 ….Forgetting to TRIM first: a leading space ends up in the first piece.
Storing lists in one column: if you split them often, a separate table with one row per item is easier to query.
PostgreSQL and SQLite
PostgreSQL has split_part, string_to_array and regexp functions; SQLite has instr and substr. Each example shows both forms; their outputs match.
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.