sqlstreak

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

PostgreSQLSQLite
AvailableYesYes
Syntaxsubstr(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

nameinitial
BeanB
BiscuitB
BrunoB
CloverC
GizmoG
JuniperJ

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

namerest
Margheritagherita
Pepperoni Rocketperoni Rocket
Four Cheese Orbitr Cheese Orbit
BBQ Meteor Meteor
Veggie Nebulagie 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

yearusers
2026300

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
From 1, three characterssubstr('Biscuit', 1, 3)'Bis'substr('Biscuit', 1, 3)'Bis'
From 4 to the endsubstr('Biscuit', 4)'cuit'substr('Biscuit', 4)'cuit'
Start at 0substr('Biscuit', 0, 3)'Bi'substr('Biscuit', 0, 3)'Bi'
Negative startsubstr('Biscuit', -3)'Biscuit'substr('Biscuit', -3)'uit'
Negative lengthsubstr('Biscuit', 3, -1)error: negative substring length not allowedsubstr('Biscuit', 3, -1)'i'
Past the endsubstr('Biscuit', 20)''substr('Biscuit', 20)''
Accented letters count as onesubstr('Tomás', 4, 2)'ás'substr('Tomás', 4, 2)'ás'
NULLsubstr(NULL, 1, 2)NULLsubstr(NULL, 1, 2)NULL

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

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