string

REPLACE

Replaces every occurrence of from_str inside source with to_str. Case-sensitive in Postgres and SQLite; MySQL follows the column's collation.

REPLACE(source, from_str, to_str)

Parameters / Modifiers

Parameter Purpose
REGEXP_REPLACE Pattern-based replacement (Postgres, MySQL 8, external extension in SQLite)
TRANSLATE(str, from, to) Postgres: single-char-for-char replacement in one pass
'g' flag REGEXP_REPLACE global flag to replace every match (Postgres)

Examples

SELECT REPLACE('foo-bar-baz', '-', '_');

Returns 'foo_bar_baz'

UPDATE users SET email = REPLACE(email, '@old.com', '@new.com');

Mass-migrate email domains

SELECT REPLACE('aaaa', 'a', '');

Returns empty string — REPLACE strips every match

SELECT REGEXP_REPLACE('abc123def', '[0-9]+', '#');

Regex-driven replacement returning 'abc#def'

Dialect notes / Gotcha

REPLACE(NULL, ...) returns NULL — wrap with COALESCE if you need pass-through. Not regex — use REGEXP_REPLACE for patterns. Note: REGEXP_REPLACE is Postgres + MySQL 8+ only. SQLite has no built-in regex functions — install the pcre/ICU extension.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet