REPLACE() in MySQL — How It Works

What the MySQL REPLACE() function does, with an example.

Published October 18, 2026

REPLACE(str, from, to) replaces every occurrence of a substring. It is case-sensitive for binary strings and follows collation otherwise.

How it works

It returns a new value; use UPDATE to change stored data.

Try it in MySQL

SELECT REPLACE('www.old.com', 'old', 'new')
www.new.com
SELECT REPLACE('a-b-c', '-', '')
abc
SELECT REPLACE(NULL, 'a', 'b')
NULL

Practical examples

Example 1

SELECT REPLACE('www.old.com', 'old', 'new');
-- www.new.com

Example 2

SELECT REPLACE('a-b-c', '-', '');
-- abc

Example 3

SELECT REPLACE(NULL, 'a', 'b');
-- NULL

Use it in a real query

SELECT id, REPLACE('www.old.com', 'old', 'new') AS result
FROM your_table
LIMIT 10;

Common mistakes

  • Forgetting an UPDATE ... WHERE and replacing in every row.
  • Confusing with the REPLACE INTO statement.
  • Expecting regex support (use REGEXP_REPLACE in MySQL 8).

Key takeaways

  • REPLACE() in one line: REPLACE(str, from, to) replaces every occurrence of a substring.
  • Try queries in a test database first.
  • Check NULL handling and data types.

FAQ

Regex replace?

MySQL 8 offers REGEXP_REPLACE().

Does REPLACE() work in other databases?

Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.

More SQL articles