Menu

MariaDB REPLACE() Function: Syntax and Examples

Learn how MariaDB REPLACE() replaces every case-sensitive substring match, handles NULL arguments, and differs from the REPLACE INTO statement.

Posted on By
On this page

MariaDB’s REPLACE() is a string function: it returns a string with every occurrence of one literal substring replaced by another. It does not update a table row by itself. See the official MariaDB REPLACE() function reference.

Syntax

REPLACE(str, from_str, to_str)
  • str is the input string or string expression.
  • from_str is the literal text to find.
  • to_str is the text to substitute for each match.

If any argument is NULL, the result is NULL.

Replace every occurrence

REPLACE() substitutes all matches, not only the first one:

SELECT REPLACE('red-red', 'red', 'blue') AS result;

The result is blue-blue.

Matching is case-sensitive

MariaDB searches for from_str with a case-sensitive match:

SELECT REPLACE('MariaDB', 'M', 'm') AS changed,
       REPLACE('MariaDB', 'm', 'x') AS no_match;

The first expression returns mariaDB. The second remains MariaDB because the lowercase m does not match the uppercase M.

Use it with a table column

First preview a value for the row you intend to change:

SELECT product_id,
       description,
       REPLACE(description, 'Old label', 'New label') AS preview
FROM products
WHERE product_id = 42;

If the preview is correct, use the same expression in an UPDATE:

UPDATE products
SET description = REPLACE(description, 'Old label', 'New label')
WHERE product_id = 42;

The WHERE clause limits the update to the selected row. REPLACE() changes text in the expression; the UPDATE statement is what writes the result to the table.

REPLACE() function vs. REPLACE INTO statement

These have different jobs. REPLACE() transforms string values. The REPLACE statement inserts a row, or deletes a conflicting row and inserts a new one when a primary or unique key matches. That delete-and-insert behavior can affect triggers, foreign keys, and auto-increment values. If you want to update selected columns on a key conflict, see the MariaDB UPSERT guide for INSERT ... ON DUPLICATE KEY UPDATE.

For pattern-based substitutions instead of literal substring matches, see MariaDB’s REGEXP_REPLACE() reference.