How7o
  • Home
  • Tools
  • Prank Screens
  • Contact
  • Blog
Reading: How to Extract Only the Digits from a String 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 Extract Only the Digits from a String in MySQL
Web Development

How to Extract Only the Digits from a String in MySQL

how7o
By how7o
Last updated: May 10, 2026
6 Min Read
MySQL extract digits from string — REGEXP_REPLACE negation class
SHARE

To mysql extract digits from string — pulling 123456 out of abc123def456 — use REGEXP_REPLACE with the [^0-9] class in a SELECT. The class negation strips everything that isn’t a digit, leaving only the numeric characters. This guide covers the basic select, the preserve-sign-and-decimal variant, an UPDATE that writes the cleaned value back, and generated-column caching for hot paths.

Contents
  • TL;DR
  • How the regex works
  • Preserving minus sign and decimal point
  • Updating the column in place
  • Generated column (MySQL 8.0+)
  • MySQL 5.7 and earlier
  • 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

SELECT REGEXP_REPLACE(string_column, '[^0-9]', '') AS digits
FROM table_name;

How the regex works

  • [0-9] — matches any single digit.
  • [^0-9] — matches any character that is not a digit (the ^ at the start of a character class negates it).
  • REGEXP_REPLACE(str, pattern, replacement) — replaces every pattern match with the replacement. Passing an empty string as replacement deletes those characters.

Put together: every non-digit character is removed. "abc123def456" → "123456".

mysql extract digits from string — REGEXP_REPLACE with [^0-9] strips non-digit characters

Preserving minus sign and decimal point

-- "USD -12.50" -> "-12.50"
SELECT REGEXP_REPLACE(string_column, '[^-0-9.]', '') AS numeric_value
FROM table_name;

-- Cast to decimal for math
SELECT CAST( REGEXP_REPLACE(string_column, '[^-0-9.]', '') AS DECIMAL(10, 2) ) AS value
FROM table_name;

For currency-string cleanup (see also PHP string-to-float for the same pattern server-side), include - and . in the keep-set. The final CAST turns the cleaned string into a proper decimal you can use in WHERE or SUM.

Updating the column in place

-- Test on a small slice first
UPDATE table_name
SET string_column = REGEXP_REPLACE(string_column, '[^0-9]', '')
WHERE id < 100;

-- Then run the full update
UPDATE table_name
SET string_column = REGEXP_REPLACE(string_column, '[^0-9]', '');

Destructive — once you’ve replaced "abc123" with "123" the original letters are gone. Take a backup first, and run against a WHERE id < 100 sample to confirm the transform looks correct before touching the whole table.

Generated column (MySQL 8.0+)

ALTER TABLE table_name
ADD COLUMN digits_only VARCHAR(255)
    GENERATED ALWAYS AS (REGEXP_REPLACE(string_column, '[^0-9]', ''))
    STORED;

-- Query the pre-computed column
SELECT digits_only FROM table_name WHERE id = 42;

When you need the extracted digits repeatedly (filtering, joining, indexing), a STORED generated column computes it once on write and caches the result. REGEXP_REPLACE runs only on INSERT / UPDATE, not on every SELECT. You can also add an index on digits_only to make phone-number or invoice-number lookups fast.

MySQL 5.7 and earlier

REGEXP_REPLACE landed in MySQL 8.0. On 5.7 the best options are:

  • Upgrade — MySQL 5.7 reached end-of-life in October 2023 and no longer gets security updates. If you’re still on it, the upgrade path is the right answer.
  • Do it in the app layer — one-liner in any language. PHP: preg_replace('/\D+/', '', $value). Python: re.sub(r'\D', '', value). Adds a round trip but works on any MySQL version.
  • User-defined function — write a stored function that loops through the string character-by-character. Works, but slow and awkward.

Frequently asked questions

What’s the shortest mysql extract digits from string query?

REGEXP_REPLACE(column, '[^0-9]', ''). The [^0-9] class matches any character that is not a digit (^ inside a character class negates the set), and REGEXP_REPLACE replaces each match with an empty string. Result: only digits remain. Needs MySQL 8.0+ or MariaDB 10.0.5+.

What if I’m on MySQL 5.7 without REGEXP_REPLACE?

REGEXP_REPLACE shipped in MySQL 8.0. On 5.7 you have two options: upgrade (5.7 is EOL as of October 2023), or do the extraction in the application layer — a one-liner in PHP is preg_replace('/\D+/', '', $value). See converting a string to float in PHP for the sibling pattern on the app side.

Does this work on negative numbers or decimals?

No — the [^0-9] pattern strips everything that isn’t a digit, including minus signs and decimal points. "-1.25" becomes "125". If you need to preserve sign and decimal, use [^-0-9.] to keep those characters too: REGEXP_REPLACE(column, '[^-0-9.]', ''). Then cast to DECIMAL for math operations.

Can I update the column in place instead of selecting?

Yes — wrap the same expression in an UPDATE: UPDATE table_name SET column = REGEXP_REPLACE(column, '[^0-9]', ''). Take a database backup first — REGEXP_REPLACE is a destructive transform ("abc123def456" becomes "123456", you can’t recover abc and def from the result). Test with a WHERE id < 100 first.

Is a computed column an option?

MySQL 8.0 supports generated columns: ALTER TABLE table_name ADD COLUMN digits_only VARCHAR(255) GENERATED ALWAYS AS (REGEXP_REPLACE(original_column, '[^0-9]', '')) STORED. The value auto-updates whenever the source column changes. Useful when you need the extracted value repeatedly and the extraction cost would otherwise hit every query.

Related guides

  • How to Remove a Specific String from a Column in MySQL — the sibling REPLACE / REGEXP_REPLACE pattern.
  • How to Combine Multiple Columns as One String in MySQL — composite string operations.
  • How to Convert a String to Float in PHP — the app-layer equivalent for currency strings.
  • How to Fix “Unknown column ‘CONCAT'” in Laravel — using MySQL string functions from Eloquent.

References

MySQL REGEXP_REPLACE docs: 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 WooCommerce remove checkout fields — woocommerce_checkout_fields filter unsetting fields How to Remove Checkout Fields in WooCommerce
Next Article MySQL remove string from column — REPLACE and REGEXP_REPLACE patterns How to Remove a Specific String from a Column in MySQL
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

Laravel Eloquent exists method checking if a record exists in a database query
Web Development

How to Check if a Record Exists in Laravel

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

How to Get the Current Category ID in WordPress

7 Min Read
Display only the current date in Laravel using Carbon
Web Development

How to Display Only the Current Date in Laravel (Carbon Examples)

4 Min Read
Laravel Eloquent multiple where and orWhere — closure-grouped query snippet with parenthesis highlight
Web Development

How to Combine Multiple where() and orWhere() in Laravel Eloquent

7 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?