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

DatabaseMySQLMySQLi

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)
raja
Published on 04-Oct-2019 08:16:26
Advertisements