Sub topic 9
Replacing text with `replace`, `translate` and `overlay`
Assume you know basic SELECT expressions and PostgreSQL NULL behavior. These functions transform text inside a query. They do not change the stored value unless you use them in UPDATE.
Choosing the right function
Use replace when you want to substitute every occurrence of one substring. Use translate when you want to map individual characters. Use overlay when you want to replace a section at a known position.
| Function | Main job | Example result |
|---|---|---|
| `replace` | Replace matching substrings | `replace('a-b-c', '-', '/')` → `a/b/c` |
| `translate` | Map or remove characters | `translate('123-456', '-', '')` → `123456` |
| `overlay` | Replace text at a position | `overlay('PostgreSQL' placing 'SQL' from 9)` → `PostgreSQL` |
Replacing complete substrings
replace(source, old_text, new_text) finds every non-overlapping occurrence of old_text and substitutes new_text.
SELECT replace('a-b-c', '-', '/') AS normalized_path,
replace(' Ana ', ' ', '') AS without_spaces,
replace('PostgreSQL', 'SQL', ' database') AS changed;The function replaces all matches, not only the first one. If old_text is an empty string, PostgreSQL inserts the replacement between characters and at both ends, so avoid that input unless this behavior is intentional.
A NULL argument produces NULL. If a missing replacement should mean “remove the text,” use an empty string explicitly. If a missing source should instead become empty text, handle it first:
SELECT replace(coalesce(comment, ''), '[draft]', '') AS published_comment
FROM messages;coalesce returns the first non-NULL value.
Mapping or removing characters
translate(source, from, to) examines each character in source. A character found at position 1 in from changes to the character at position 1 in to. Characters in from with no corresponding character in to are removed.
SELECT translate('Phone: (555) 123-4567',
' ()-',
'') AS digits_only,
translate('abc123', 'abc', 'ABC') AS upper_letters;The first query removes spaces, parentheses, and the hyphen. The second maps a to A, b to B, and c to C.
translate works character by character. It cannot replace a multi-character word with another word. Use replace for that. Repeated characters in from do not create repeated mappings. The first occurrence determines the mapping.
Replacing text by position
overlay replaces a section beginning at a one-based character position:
SELECT overlay('PostgreSQL'
placing 'SQL'
from 9
for 0) AS appended,
overlay('2026-01-27'
placing '11'
from 6
for 2) AS corrected_date;The for value is the number of characters removed. With for 0, the replacement is inserted without removing existing characters. If for is omitted, PostgreSQL removes as many characters as the replacement contains.
Positions count characters, not bytes. This matters for text containing characters such as é or emoji. A position can also be beyond the end of the source, in which case the replacement is placed at the end.
Exercise
Which function should convert AB-123-CD to AB123CD, and which expression does it use?
Show answer
Use translate, because the unwanted character is a single character: translate('AB-123-CD', '-', '').