sqlstreak

SQL function

REPLACE() in SQL: swap text inside a string

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

REPLACE(text, from, to) returns the text with every occurrence of from swapped for to: REPLACE('555-0101', '-', '') is '5550101'. It's case-sensitive and the same in PostgreSQL and SQLite.

Syntax and availability

PostgreSQLSQLite
AvailableYesYes
SyntaxREPLACE(text, from_text, to_text)REPLACE(text, from_text, to_text)

Examples

Phone numbers without the dash

SELECT id, phone, REPLACE(phone, '-', '') AS digits
FROM adoption_requests
WHERE NULLIF(TRIM(phone), '') IS NOT NULL
ORDER BY id
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

idphonedigits
1555-01015550101
4555-01045550104
5555-01055550105
7555-01075550107
9555-01095550109

Try it: PostgreSQL playground · SQLite playground

URL slugs from names

SELECT name, LOWER(REPLACE(name, ' ', '-')) AS slug
FROM menu_items
ORDER BY id
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

nameslug
Margheritamargherita
Pepperoni Rocketpepperoni-rocket
Four Cheese Orbitfour-cheese-orbit
BBQ Meteorbbq-meteor
Veggie Nebulaveggie-nebula

Try it: PostgreSQL playground · SQLite playground

Several replacements: nest them

SELECT name, REPLACE(REPLACE(LOWER(name), ' ', '_'), '&', 'and') AS key
FROM staff
ORDER BY id
LIMIT 4;

Result (PostgreSQL and SQLite, identical): 4 rows

namekey
Rosa Vegarosa_vega
Omar Haddadomar_haddad
Lia Moreaulia_moreau
Ben Carterben_carter

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
Every occurrenceREPLACE('a-b-c', '-', '')'abc'REPLACE('a-b-c', '-', '')'abc'
Case mattersREPLACE('Ab', 'a', 'x')'Ab'REPLACE('Ab', 'a', 'x')'Ab'
Overlapping matchesREPLACE('aaa', 'aa', 'b')'ba'REPLACE('aaa', 'aa', 'b')'ba'
Searching for an empty stringREPLACE('abc', '', 'x')'abc'REPLACE('abc', '', 'x')'abc'
Not foundREPLACE('abc', 'z', 'x')'abc'REPLACE('abc', 'z', 'x')'abc'
NULL replacementREPLACE('abc', 'b', NULL)NULLREPLACE('abc', 'b', NULL)NULL
NULL textREPLACE(NULL, 'a', 'b')NULLREPLACE(NULL, 'a', 'b')NULL

Common mistakes

  • Expecting a case-insensitive replace: REPLACE(LOWER(x), …) changes the case of everything; PostgreSQL's regexp_replace(x, 'a', 'b', 'gi') keeps it.

  • A NULL replacement: the whole result becomes NULL, not just the match.

  • Confusing it with SQLite's REPLACE INTO … statement, which is an upsert, not a text function.

PostgreSQL and SQLite

Everything on this page works the same in PostgreSQL and SQLite: each example above was run on both, and the results match.

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