Function Reference

REPLACE

Replaces all occurrences of the substring from in a string with the substring to.

PostgreSQL 17.9
REPLACE(string, from, to)
string
Source string
text
from
Substring to replace
text
to
Replacement substring, an empty string removes the occurrences
text

Return value

A string with occurrences replaced; the search is case-sensitive

Examples

Replacing a substring

PostgreSQL 17.9
SELECT REPLACE('abcdef', 'cd', 'XX');
replace
abXXef

Replacing all separators in a date

PostgreSQL 17.9
SELECT REPLACE('2024-03-08', '-', '.');
replace
2024.03.08

Removing a word from names

PostgreSQL 17.9
SELECT good_name,
	REPLACE(good_name, ' fee', '') AS short_name
FROM Goods
WHERE good_name LIKE '% fee';
good_nameshort_name
apartment feeapartment
phone feephone
music school feemusic school
english school feeenglish school

Details

The function looks for a plain substring, not a pattern: for regular expressions there is REGEXP_REPLACE, and for replacing single characters, TRANSLATE. An empty from changes nothing.

See also