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
| PostgreSQL | SQLite | |
|---|---|---|
| Available | Yes | Yes |
| Syntax | REPLACE(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
| id | phone | digits |
|---|---|---|
| 1 | 555-0101 | 5550101 |
| 4 | 555-0104 | 5550104 |
| 5 | 555-0105 | 5550105 |
| 7 | 555-0107 | 5550107 |
| 9 | 555-0109 | 5550109 |
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
| name | slug |
|---|---|
| Margherita | margherita |
| Pepperoni Rocket | pepperoni-rocket |
| Four Cheese Orbit | four-cheese-orbit |
| BBQ Meteor | bbq-meteor |
| Veggie Nebula | veggie-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
| name | key |
|---|---|
| Rosa Vega | rosa_vega |
| Omar Haddad | omar_haddad |
| Lia Moreau | lia_moreau |
| Ben Carter | ben_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.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Every occurrence | REPLACE('a-b-c', '-', '')'abc' | REPLACE('a-b-c', '-', '')'abc' |
| Case matters | REPLACE('Ab', 'a', 'x')'Ab' | REPLACE('Ab', 'a', 'x')'Ab' |
| Overlapping matches | REPLACE('aaa', 'aa', 'b')'ba' | REPLACE('aaa', 'aa', 'b')'ba' |
| Searching for an empty string | REPLACE('abc', '', 'x')'abc' | REPLACE('abc', '', 'x')'abc' |
| Not found | REPLACE('abc', 'z', 'x')'abc' | REPLACE('abc', 'z', 'x')'abc' |
| NULL replacement | REPLACE('abc', 'b', NULL)NULL | REPLACE('abc', 'b', NULL)NULL |
| NULL text | REPLACE(NULL, 'a', 'b')NULL | REPLACE(NULL, 'a', 'b')NULL |
Related
Common mistakes
Expecting a case-insensitive replace:
REPLACE(LOWER(x), …)changes the case of everything; PostgreSQL'sregexp_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
- 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.