How to extract only the numbers from a text field in MySQL?



Let us first create a table −

mysql> create table DemoTable (    Number text ); Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('7364746464,-'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('-,8909094556'); Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+ | Number | +--------------+ | 7364746464,- | | -,8909094556 | +--------------+ 2 rows in set (0.00 sec)

Following is the query to extract numbers from a text field in MySQL −

mysql> select replace(replace(replace(replace(Number, '-', ''), ' ', ''),'"',''),',','') as Result from DemoTable;

This will produce the following output −

+------------+ | Result | +------------+ | 7364746464 | | 8909094556 | +------------+ 2 rows in set (0.00 sec)
Updated on: 2019-10-04T08:23:53+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements