How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Remove a Specific String from a Column in MySQL
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Web Development > How to Remove a Specific String from a Column in MySQL
Web Development

How to Remove a Specific String from a Column in MySQL

how7o
By how7o
Last updated: May 10, 2026
6 Min Read
MySQL remove string from column — REPLACE and REGEXP_REPLACE patterns
SHARE

Two functions handle mysql remove string from column: REPLACE for literal substrings, REGEXP_REPLACE (MySQL 8+) for patterns. Use REPLACE when the target is a fixed string — a deprecated domain, a tracking prefix, a stale watermark. Use REGEXP_REPLACE when the target varies — any digit, any non-alphanumeric, any character outside an allow-list. This guide covers both and the preview-then-update workflow that keeps mass updates safe.

Contents
  • TL;DR
  • Literal strings with REPLACE
  • Patterns with REGEXP_REPLACE
  • Preview before UPDATE
  • Case sensitivity
  • Back up first
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on MySQL 8.0 and MariaDB 10.11. Originally published 2022-12-20, rewritten and updated 2026-04-23.

TL;DR

-- Literal string: remove "http://old-domain.com/" wherever it appears
UPDATE table_name
SET column_name = REPLACE(column_name, 'http://old-domain.com/', '')
WHERE column_name LIKE '%http://old-domain.com/%';

-- Pattern: strip every non-alphanumeric character
UPDATE table_name
SET column_name = REGEXP_REPLACE(column_name, '[^A-Za-z0-9]', '');

Literal strings with REPLACE

UPDATE table_name
SET column_name = REPLACE(column_name, 'substring_to_remove', '')
WHERE condition;

REPLACE(str, search, replace) replaces every occurrence of search with replace. Passing '' as the replacement deletes. Works on any MySQL/MariaDB version — no regex engine, no version gate.

The WHERE column_name LIKE '%substring_to_remove%' clause is optional but wise: it limits the UPDATE to rows that actually contain the target, reducing the number of modified rows and the risk of an accidental rewrite.

mysql remove string from column — REPLACE for literal strings, REGEXP_REPLACE for patterns

Patterns with REGEXP_REPLACE

UPDATE table_name
SET column_name = REGEXP_REPLACE(column_name, 'pattern_to_match', '')
WHERE condition;

Common patterns:

  • [a-z] — any lowercase letter.
  • [0-9] — any digit.
  • [A-Za-z0-9] — any alphanumeric character.
  • [^A-Za-z0-9] — any non-alphanumeric character.
  • \\d+ — one or more consecutive digits.
  • \\w+ — one or more consecutive word characters (letters, digits, underscore).
  • \\s+ — one or more whitespace characters.

Combine with quantifiers (+, *, {n,m}) for length-bounded matches. See extracting only digits for the inverse “keep instead of remove” approach.

Preview before UPDATE

-- See what the UPDATE would do, without writing
SELECT
    id,
    column_name                              AS before_value,
    REPLACE(column_name, 'target', '')       AS after_value
FROM table_name
WHERE column_name LIKE '%target%'
LIMIT 50;

Always check the transform against 50–100 rows before running on the whole table. It’s the cheapest way to catch a pattern mistake (e.g. forgetting a trailing slash in a URL would match and rewrite more than you intended).

Case sensitivity

-- Column's default collation: matches "Target", "TARGET", "target"
UPDATE t SET col = REPLACE(col, 'Target', '');

-- Force byte-exact matching (case-sensitive)
UPDATE t SET col = REPLACE(BINARY col, 'Target', '');

MySQL’s default utf8mb4_unicode_ci collation is case-insensitive, so REPLACE treats "Target" and "target" as the same string. Wrap the column in BINARY to get byte-level matching when you need the exact casing.

Back up first

# Command-line backup before a mass UPDATE
mysqldump -u root -p mydb table_name > table_backup.sql

A mass UPDATE with REPLACE or REGEXP_REPLACE can’t be undone without a backup. For table-level snapshots, mysqldump is the standard — see exporting and importing MySQL databases for the full-database version.

Frequently asked questions

What’s the simplest mysql remove string from column query?

UPDATE table SET col = REPLACE(col, 'to_remove', ''). REPLACE swaps every occurrence of the literal target string with the replacement — pass an empty replacement to delete. Works on any MySQL/MariaDB version; no regex engine needed.

When should I use REGEXP_REPLACE instead?

When the string to remove isn’t a fixed literal — you want to strip all digits, any non-alphanumeric character, or anything matching a pattern. REGEXP_REPLACE takes a regex as the target; REPLACE takes a literal string. REGEXP_REPLACE requires MySQL 8.0+ or MariaDB 10.0.5+.

Is the REPLACE function case-sensitive?

Depends on the column’s collation. Columns with a _ci collation (case-insensitive — the default for utf8mb4_unicode_ci) match case-insensitively; _cs or _bin collations are case-sensitive. For explicit control, wrap in BINARY to force a byte-level comparison: REPLACE(BINARY col, 'Target', '') only matches the exact casing.

Can I preview the changes before committing?

Yes — run the equivalent SELECT first: SELECT id, column_name, REPLACE(column_name, 'to_remove', '') AS preview FROM table_name WHERE column_name LIKE '%to_remove%'. Inspect the output, then run the UPDATE. Always back up the table before a mass string update — REPLACE is destructive.

Why does my regex not match special characters?

MySQL’s REGEXP_REPLACE uses POSIX-style regex with a couple of MySQL-specific rules: double-escape literal backslashes ('\\\\.' to match a literal dot), and use single-quoted strings with care because MySQL’s own string escaping runs first. For complex patterns test them in a minimal SELECT before wrapping in UPDATE — a typo in the pattern that matches nothing is better than one that matches too much.

Related guides

  • How to Extract Only the Digits from a String in MySQL — the inverse “keep only matching characters” pattern.
  • How to Combine Multiple Columns as One String in MySQL — string building with CONCAT.
  • How to Export and Import All MySQL Databases at Once — backup before mass updates.
  • How to Prepare a %LIKE% SQL Statement in WordPress — the sibling WordPress pattern.

References

MySQL string functions: dev.mysql.com/doc/refman/8.0/en/string-functions. REGEXP_REPLACE: dev.mysql.com/doc/refman/8.0/en/regexp.

TAGGED:mysqlsql

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
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 MySQL extract digits from string — REGEXP_REPLACE negation class How to Extract Only the Digits from a String in MySQL
Next Article MariaDB restart on Linux — systemctl restart mariadb How to Restart the MariaDB Server on Linux
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Display PHP errors — ini_set + php.ini configuration
How to Display PHP Errors
May 10, 2026
PHP convert string to uppercase — strtoupper and mb_strtoupper
How to Convert a String to Uppercase in PHP
May 10, 2026
PHP string to float conversion with cast, regex cleanup, NumberFormatter
How to Convert a String to Float in PHP
May 10, 2026
PHP merge arrays without duplicates — union operator and array_unique
How to Combine Two Arrays Without Duplicates in PHP
May 10, 2026
PHP delete array element — unset, array_splice, array_filter, array_search
How to Delete an Element from a PHP Array
May 10, 2026

You Might Also Like

WooCommerce add custom fee — woocommerce_cart_calculate_fees + WC()->cart->add_fee
Web Development

How to Add a Custom Fee (or Transaction Fee) in WooCommerce

8 Min Read
WordPress disable revision and autosave — wp-config constants and wp_print_scripts dequeue
Web Development

How to Disable Revisions and Autosave in WordPress

8 Min Read
Laravel updateOrCreate method shown in an Eloquent code snippet with insert and update branches
Web Development

Laravel updateOrCreate: Insert or Update Records in Eloquent

8 Min Read
Laravel left outer join — query builder leftJoin illustration with two tables
Web Development

How to Do a Left Outer Join in Laravel Query Builder

8 Min Read
How7o

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

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?