MariaDB REPLACE() function: find and replace text in a string
In MariaDB, REPLACE(source, search, replacement) returns a string with every exact match replaced. Matching is case sensitive. Use the function in SELECT to see the result, or in the SET clause of UPDATE to change stored values.
Understand the three arguments and basic results
SELECT REPLACE('ababab', 'b', 'c') AS all_matches,
REPLACE('abc ABC abc', 'abc', 'xyz') AS case_sensitive,
REPLACE('a-b-c', '-', '') AS remove_hyphens,
REPLACE('abc', 'x', 'y') AS not_found,
REPLACE('abc', '', 'X') AS empty_search,
REPLACE('abc', 'a', NULL) AS null_replacement;
Run the first SELECT without a table. The results are acacac, xyz ABC xyz, abc, abc, abc, and NULL. An empty replacement removes the matching text. In the example’s ordinary SQL mode, a missing match or empty search string returns the original. A NULL argument makes the result NULL. MariaDB Docs · MariaDB REPLACE()
Compare literal, pattern-based, and chained replacement
SELECT REPLACE('a.b.c', '.', '-') AS literal_dot,
REPLACE('aaa', 'aa', 'X') AS overlapping_input,
REPLACE('a', 'a', 'aa') AS longer_replacement,
REPLACE(REPLACE('ab', 'a', 'b'), 'b', 'c') AS chained;
SELECT REGEXP_REPLACE('ab12cd34', '[0-9]+', 'X') AS pattern_match;
In the second query, the dot is literal, so the result is a-b-c. Replacing aa in aaa gives Xa because overlapping matches are not scanned again. Replacing a with aa returns aa; newly inserted text is left alone in that call. Nesting calls processes each intermediate result, so ab becomes bb and then cc.
Use REGEXP_REPLACE() to find a pattern such as consecutive digits. The example transforms ab12cd34 into abXcdX. Check the effective collation and pattern options when case sensitivity matters. For one range with a known start and length, use the position-based INSERT() function. MariaDB Docs · MariaDB REGEXP_REPLACE() MariaDB INSERT()
Match the filter to case-sensitive replacement
Use the learning practice database with CREATE, INSERT, SELECT, and UPDATE privileges. If the table exists, use a fresh practice database. Its text column explicitly uses the case-insensitive utf8mb4_unicode_ci collation. Start by previewing the target rows and the calculated values on sample data.
USE learning;
CREATE TABLE replace_text_demo (
id INT PRIMARY KEY,
text_value VARCHAR(200) COLLATE utf8mb4_unicode_ci NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO replace_text_demo VALUES
(1, 'abc abc'), (2, 'ABC abc'), (3, 'ABC'),
(4, NULL), (5, 'unchanged');
SELECT id, text_value AS before_value,
REPLACE(text_value, 'abc', 'xyz') AS after_value
FROM replace_text_demo
WHERE BINARY text_value LIKE '%abc%'
ORDER BY id;
SELECT COUNT(CASE WHEN text_value LIKE '%abc%' THEN 1 END) AS like_matches,
COUNT(CASE WHEN BINARY text_value LIKE '%abc%' THEN 1 END) AS binary_matches
FROM replace_text_demo;
Ordinary LIKE matches three rows, IDs 1, 2, and 3. BINARY LIKE matches only IDs 1 and 2 with lowercase abc, so the preview gives xyz xyz and ABC xyz. If an actual search string includes % or _, escape those wildcard characters in a LIKE pattern. REPLACE() treats its search argument as literal text. MariaDB Docs · MariaDB LIKE
Test UPDATE and verify the rollback
START TRANSACTION;
SELECT id, text_value
FROM replace_text_demo
WHERE BINARY text_value LIKE '%abc%'
ORDER BY id FOR UPDATE;
UPDATE replace_text_demo
SET text_value = REPLACE(text_value, 'abc', 'xyz')
WHERE BINARY text_value LIKE '%abc%';
SELECT ROW_COUNT() AS changed_rows;
SELECT id, text_value FROM replace_text_demo ORDER BY id;
ROLLBACK;
SELECT id, text_value FROM replace_text_demo ORDER BY id;
Run the block in the same connection against the InnoDB practice table. A FOR UPDATE query identifies the rows again and UPDATE applies the same filter. changed_rows is 2 and only IDs 1 and 2 change. After ROLLBACK, abc abc and ABC abc are restored. MariaDB Docs · MariaDB UPDATE MariaDB Docs · MariaDB transactions
For a real update, check the current rows and results again before COMMIT. Roll back the transaction if something fails. Recovering an already committed change requires a backup or another recovery mechanism. Verify the resulting length, unique-key constraints, character set, and locking impact, and target the rows with WHERE.
Distinguish the function from the REPLACE INTO statement
REPLACE() calculates a string value. On a key conflict, REPLACE INTO deletes an existing row and inserts a new one, which can affect defaults in other columns and DELETE or INSERT triggers. To change text in an existing column, combine UPDATE and REPLACE() as shown. MariaDB Docs · MariaDB REPLACE INTO




