SQL function
substr() in SQL (PostgreSQL and SQLite): cut part of a string
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
substr(text, start, length) returns part of a string, counting from 1: substr('Biscuit', 1, 3) is 'Bis'. Leave out the length to take everything from start to the end. The name is the same in PostgreSQL and SQLite; PostgreSQL also accepts SUBSTRING(text FROM start FOR length).
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | Yes | Yes |
| Syntax | substr(text, start [, length]) / SUBSTRING(text FROM start FOR length) | substr(text, start [, length]) / substring(text, start [, length]) |
Examples
The first letter of each name
SELECT name, substr(name, 1, 1) AS initial
FROM pets
ORDER BY name
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| name | initial |
|---|---|
| Bean | B |
| Biscuit | B |
| Bruno | B |
| Clover | C |
| Gizmo | G |
| Juniper | J |
Try it: PostgreSQL playground · SQLite playground
Everything after the first three letters
SELECT name, substr(name, 4) AS rest
FROM menu_items
ORDER BY id
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| name | rest |
|---|---|
| Margherita | gherita |
| Pepperoni Rocket | peroni Rocket |
| Four Cheese Orbit | r Cheese Orbit |
| BBQ Meteor | Meteor |
| Veggie Nebula | gie Nebula |
Try it: PostgreSQL playground · SQLite playground
The year from a date, as text
Cast the date to ISO text first; then the year is the first four characters in both databases.
SELECT substr(CAST(signed_up_on AS TEXT), 1, 4) AS year, COUNT(*) AS users
FROM users
GROUP BY 1;Result (PostgreSQL and SQLite, identical): 1 row
| year | users |
|---|---|
| 2026 | 300 |
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 |
|---|---|---|
| From 1, three characters | substr('Biscuit', 1, 3)'Bis' | substr('Biscuit', 1, 3)'Bis' |
| From 4 to the end | substr('Biscuit', 4)'cuit' | substr('Biscuit', 4)'cuit' |
| Start at 0 | substr('Biscuit', 0, 3)'Bi' | substr('Biscuit', 0, 3)'Bi' |
| Negative start | substr('Biscuit', -3)'Biscuit' | substr('Biscuit', -3)'uit' |
| Negative length | substr('Biscuit', 3, -1)error: negative substring length not allowed | substr('Biscuit', 3, -1)'i' |
| Past the end | substr('Biscuit', 20)'' | substr('Biscuit', 20)'' |
| Accented letters count as one | substr('Tomás', 4, 2)'ás' | substr('Tomás', 4, 2)'ás' |
| NULL | substr(NULL, 1, 2)NULL | substr(NULL, 1, 2)NULL |
Related
Common mistakes
Counting from 0: SQL strings start at position 1.
Relying on a negative start: SQLite counts it from the end, PostgreSQL doesn't (see the matrix).
Using substr on a date column in PostgreSQL: it wants text, so CAST the date first.
PostgreSQL and SQLite
Same name and the same answer for ordinary positions. They part ways on zero, negative starts and negative lengths: SQLite counts a negative start from the end, PostgreSQL treats positions before 1 as empty and rejects a negative length.
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.