How to extract only numbers from string in mysql?

by haylee.mertz , in category: MySQL , 9 months ago

How to extract only numbers from string in mysql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 9 months ago

@haylee.mertz 

You can use the REGEXP function in MySQL to extract only numbers from a string. Here's an example query:


SELECT REGEXP_REPLACE('abc123def456', '[^0-9]+', '') AS numbers;


In this query, the REGEXP_REPLACE function is used to replace all non-numeric characters (represented by [^0-9]+) with an empty string. The result will be a string that contains only the numbers from the original input string.


The above example will output "123456" as it extracts only the numbers from the input string "abc123def456".