Guides

Step-by-step practical fixes for everyday technical problems.
Database Guides

How to Replace Text in MySQL Column

Update repeated text patterns safely in a MySQL column without changing more rows than intended.

Before you start

Never run a bulk text replacement without previewing the affected rows first.
Always use a WHERE clause when possible so you do not update unrelated rows.
If the table matters, take a backup before doing mass text changes.

Preview the rows that will be affected

Before using UPDATE, find the rows that currently contain the old text pattern.

SQL
SELECT id, content
FROM articles
WHERE content LIKE '%old-domain.com%';
Why preview first

This lets you confirm that the target text appears exactly where you expect before any data is changed.

Replace text in one column

Use REPLACE() inside an UPDATE statement to swap the old text with the new one.

SQL
UPDATE articles
SET content = REPLACE(content, 'old-domain.com', 'new-domain.com')
WHERE content LIKE '%old-domain.com%';
What this does

It updates only rows that contain the old text and replaces every matching occurrence inside that column value.

Replace text in a URL column

A common real-world case is moving from one domain to another or fixing an old URL pattern stored in a column.

SQL
UPDATE links
SET url = REPLACE(url, 'http://oldsite.com', 'https://newsite.com')
WHERE url LIKE '%oldsite.com%';
Good use case

Useful after domain changes, SSL migration, or fixing old internal links stored in the database.

Replace text in only a subset of rows

Sometimes the old text appears in many places, but you only want to change one group of rows.

SQL
UPDATE products
SET description = REPLACE(description, 'Old Brand', 'New Brand')
WHERE category_id = 5
AND description LIKE '%Old Brand%';
Why the extra filter matters

The WHERE clause prevents a broad replacement across the entire table when only one subset should change.

Back up the table before a big replacement

If the update will affect many rows or important content, make a backup copy first.

SQL
CREATE TABLE articles_backup_20260402 AS
SELECT *
FROM articles;
Best practice

If the replacement result turns out wrong, a backup table gives you a quick recovery path.

Verify the result after the update

After running the replacement, check a few rows and confirm the old text is gone where it should be.

SQL
SELECT id, content
FROM articles
WHERE content LIKE '%new-domain.com%'
LIMIT 20;
Check both sides

Also run a quick search for the old text to make sure only the intended rows were changed and no old values remain unexpectedly.

Common real-world situations

Domain migration

Replace old URLs stored in articles, settings, or link tables after moving to a new domain.

Brand or product rename

Update repeated brand names or product labels across stored descriptions and notes.

File path cleanup

Fix old path fragments in stored file references after folder changes or server migration.

Content cleanup

Correct repeated spelling or wording patterns across a large number of stored records.

Common mistakes

Running UPDATE without previewing rows first

Always inspect the matching rows before bulk replacement so you know what will change.

Skipping the WHERE clause

A missing WHERE clause can update far more rows than intended, even if the replacement text is common.

Replacing text that is too broad

If the target string is short or generic, it may modify valid content in ways you did not expect.

Changing important data without a backup

For large or critical updates, a quick backup table is the safer move before bulk replacement.

About this guide

This guide shows how to replace text inside a MySQL column safely when you need to fix URLs, rename domains, correct repeated words, update file paths, or clean up old text patterns in bulk. It focuses on practical update work, previewing affected rows first, and avoiding mass changes that touch the wrong data.

How to follow this guide

  1. Preview the rows that contain the old text before updating anything.
  2. Use REPLACE() inside an UPDATE statement to change the text.
  3. Limit the update with a WHERE clause so only intended rows are changed.
  4. Back up the table first if the column contains important data.
  5. Verify the result after the update before moving on to other changes.

Why use this method?

Bulk text replacement is one of the fastest ways to clean or migrate data, but it is also easy to run too broadly and change more rows than intended. A safe replace workflow prevents unnecessary cleanup and rollback work.

Frequently Asked Questions

What does REPLACE() do in MySQL?

It returns a string where every occurrence of the target text is replaced with the new text.

Can I replace text in only some rows?

Yes. Use a WHERE clause so the UPDATE only touches the rows you want.

Will REPLACE() update all matching occurrences inside the same value?

Yes. Every matching occurrence in that string value is replaced.

Should I preview the affected rows before updating?

Yes. Preview first so you know exactly what will change before running a bulk update.