如何在 MySQL 中僅從文字欄位提取數字?


我們首先建立一個表 −

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

使用 insert 命令在表中插入一些記錄 −

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)

使用 select 語句從表中顯示所有記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

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

以下是 MySQL 中從文字欄位提取數字的查詢 −

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

這將產生以下輸出 −

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

更新日期: 2019 年 10 月 4 日

1,000 多次瀏覽

啟動您的職業

完成課程並獲得認證

開始
廣告