# How to split a string in SQL (PostgreSQL and SQLite)

Source: https://sqlstreak.com/sql/how-to/split-string · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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:

```sql
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:

```sql
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 (both engines):

| email | 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 |

### First and last name

Split at the first space. Names with more than two parts need more care.

PostgreSQL:

```sql
SELECT name,
       split_part(name, ' ', 1) AS first_name,
       substr(name, strpos(name, ' ') + 1) AS last_name
FROM staff
ORDER BY id;
```

SQLite:

```sql
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 (both engines):

| 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 |

### A list into rows

PostgreSQL unnests an array; SQLite walks the string with a recursive CTE.

PostgreSQL:

```sql
SELECT unnest(string_to_array('mobile,desktop,tv', ',')) AS device;
```

SQLite:

```sql
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 (both engines):

| device |
| --- |
| mobile |
| desktop |
| tv |

## How it works

- `split_part(x, '@', 2)`: PostgreSQL: the second piece of x when cut at every @.
- `instr(x, '@')`: SQLite: the position of the first @ (strpos in PostgreSQL).
- `substr(x, position + 1)`: Everything after it.

## 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.
