How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Replace Strings in a MySQL Database
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Replace Strings in a MySQL Database
Web Development

How to Replace Strings in a MySQL Database

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
Replace strings in a MySQL database with UPDATE and REPLACE()
SHARE

To replace strings in a MySQL database, use the built-in REPLACE() function inside an UPDATE statement. It replaces every occurrence of a literal substring in a column — useful for fixing typos across many rows, renaming references, or scrubbing data.

Contents
  • The basic syntax
  • WordPress post-content example
  • Always back up first
  • Preview before you commit
  • Pattern-based replace with REGEXP_REPLACE
  • Don’t use REPLACE on serialized data
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on MySQL 8.0 and MariaDB 10.11. Originally published 2022-06-23, rewritten and updated 2026-05-17.

The basic syntax

UPDATE table_name
SET    column_name = REPLACE(column_name, 'old_string', 'new_string')
WHERE  column_name LIKE '%old_string%';

REPLACE(haystack, needle, replacement) returns the haystack with every occurrence of the needle swapped out. The WHERE … LIKE filter limits the update to rows that actually contain the string — much cheaper than touching every row and rewriting it as-is.

MySQL REPLACE in UPDATE — basic syntax, WHERE LIKE filter, REGEXP_REPLACE, WordPress serialized warning

WordPress post-content example

-- Replace "Mr. John Doe" with "Mr. Phillip White" across all posts
UPDATE wp_posts
SET    post_content = REPLACE(post_content, 'Mr. John Doe', 'Mr. Phillip White')
WHERE  post_content LIKE '%Mr. John Doe%';

Run this once to update every post that mentions the old name. The LIKE filter is a performance optimisation — without it, MySQL still has to read and rewrite every row, even rows where REPLACE doesn’t change anything.

Always back up first

# Logical backup of the affected database
mysqldump -u root -p mydb > mydb-before-replace-$(date +%F).sql

# Or just the affected table
mysqldump -u root -p mydb wp_posts > wp_posts-before-replace.sql

The replace is irreversible once committed — if you replace the wrong substring, only your backup will save you.

Preview before you commit

-- See what the replacement would look like, without writing
SELECT id, post_title,
       REPLACE(post_content, 'Mr. John Doe', 'Mr. Phillip White') AS new_content
FROM   wp_posts
WHERE  post_content LIKE '%Mr. John Doe%'
LIMIT  5;

A SELECT with the same REPLACE expression shows the result without modifying the row. Spot-check a few before running the real UPDATE.

Pattern-based replace with REGEXP_REPLACE

-- MySQL 8.0+, MariaDB 10.0.5+
UPDATE wp_posts
SET    post_content = REGEXP_REPLACE(post_content,
                       '\\bMr\\. (John Doe|Jane Smith)\\b',
                       'Mr. Phillip White')
WHERE  post_content REGEXP '\\bMr\\. (John Doe|Jane Smith)\\b';

Use REGEXP_REPLACE when you need pattern matching (alternations, word boundaries, character classes) rather than a fixed substring.

Don’t use REPLACE on serialized data

WordPress stores some option values as PHP-serialized strings, where each segment is prefixed with its byte length (s:11:"hello world"). Replacing inside those values changes the byte count and breaks unserialize. For WordPress URL updates, use the WP-CLI command which handles serialization correctly:

wp search-replace 'https://old.example.com' 'https://new.example.com' \
    --all-tables --skip-columns=guid

Frequently asked questions

Does REPLACE use a regex or a literal string?

Literal substring. REPLACE(col, 'Mr. John Doe', 'Mr. Phillip White') matches that exact sequence of characters — no wildcards, no regex. For pattern-based replacement, use REGEXP_REPLACE (MySQL 8.0+, MariaDB 10.0.5+) with a regex pattern.

Why add WHERE col LIKE '%...%' when REPLACE is a no-op on rows that don’t contain the string anyway?

Two reasons. Performance: without the WHERE, every row is updated (its UPDATE timestamp changes, write logs grow, replication ships every row). With the filter, only matching rows are touched. Safety: a stray run with the wrong arguments can’t blast through your entire table if you’ve scoped it to known-matching rows.

How do I do a case-insensitive replace?

REPLACE is case-sensitive on case-sensitive collations (like utf8mb4_bin) and case-insensitive on the common utf8mb4_general_ci / utf8mb4_unicode_ci. If you need case-insensitive matching regardless of column collation, use REGEXP_REPLACE(col, 'mr\\. john doe', 'Mr. Phillip White', 1, 0, 'i') — the 'i' flag forces case-insensitivity.

How do I update WordPress URLs in serialized data?

Don’t use plain REPLACE on serialized strings — the s:N: length prefix becomes wrong after replacement, breaking the unserialize. Use a WordPress-specific tool: the WP-CLI command wp search-replace 'oldurl' 'newurl' --all-tables handles serialized data correctly. For non-WP databases, write a script in your application language that round-trips through unserialize/serialize.

Related guides

  • How to Combine Multiple Columns as One String in MySQL
  • How to Get Only the Digits from a String in MySQL
  • How to Remove a Specific String from a Column in MySQL

References

MySQL REPLACE(): dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_replace. MySQL REGEXP_REPLACE(): dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-replace. WP-CLI search-replace: developer.wordpress.org/cli/commands/search-replace.

TAGGED:mysqlsqlwordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Rename menu items on the WooCommerce My Account page How to Rename Menu Items on the WooCommerce My Account Page
Next Article Reset the LiteSpeed WebAdmin Console password How to Reset the LiteSpeed WebAdmin Console Password
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

WordPress get current category ID — three methods by page context
Web Development

How to Get the Current Category ID in WordPress

7 Min Read
WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter
Web Development

How to Dynamically Change Currency in WooCommerce

7 Min Read
Get a favicon URL with JavaScript
Web Development

How to Get a Website’s Favicon URL with JavaScript

4 Min Read
jQuery check if a checkbox is checked
Web Development

How to Check if a Checkbox Is Checked with jQuery

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?