如何在 MySQL 中從字串中獲取最後 12 位數字?


可以使用 MySQL 中的 RIGHT() 函式從字串中獲取最後 12 位數字。我們首先建立一個表 -

mysql> create table DemoTable
   (
   Number varchar(200)
   );
Query OK, 0 rows affected (0.59 sec)

使用 insert 命令在表中插入記錄 -

mysql> insert into DemoTable values('7437647847847474374747464647484949959958484');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('9990000399494959697080800007007070808080808');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('1211111212344554444443333345555554433333333333333');
Query OK, 1 row affected (0.14 sec)

使用 select 語句顯示錶中的所有記錄 -

mysql> select * from DemoTable;

這將產生以下輸出 -

+---------------------------------------------------+
| Number                                            |
+---------------------------------------------------+
| 7437647847847474374747464647484949959958484       |
| 9990000399494959697080800007007070808080808       |
| 1211111212344554444443333345555554433333333333333 |
+---------------------------------------------------+
3 rows in set (0.00 sec)

以下是 MySQL 中從字串中獲取最後 12 位數字的查詢 -

mysql> select right(Number,12) as `Last 12 Digit From String` from DemoTable;

這將產生以下輸出 -

+---------------------------+
| Last 12 Digit From String |
+---------------------------+
| 949959958484              |
| 070808080808              |
| 333333333333              |
+---------------------------+
3 rows in set (0.00 sec)

更新於: 2019-07-30

754 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告