如何從 MySQL 字串中提取日期?


要從 MySQL 中的字串中提取日期,請使用 SUBSTRING_INDEX()。我們先建立一個表 -

mysql> create table DemoTable
   -> (
   -> Title text
   -> );
Query OK, 0 rows affected (0.58 sec)

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

mysql> insert into DemoTable values('John has got joining date.12/31/2018');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Carol has got joining date.01/11/2019');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Sam will arrive at.12/03/2050');
Query OK, 1 row affected (0.87 sec)

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

mysql> select *from DemoTable;

這將產生以下輸出 -

+---------------------------------------+
| Title                                 |
+---------------------------------------+
| John has got joining date.12/31/2018  |
| Carol has got joining date.01/11/2019 |
| Sam will arrive at.12/03/2050         |
+---------------------------------------+
3 rows in set (0.00 sec)

以下是從 MySQL 中的字串中提取日期的查詢 -

mysql> select substring_index(substring_index(Title,".", -1), ".", 1) from DemoTable;

這將產生以下輸出 -

+----------------------------------------------------------+
| substring_index(substring_index(Title,".", -1), ".", 1)  |
+----------------------------------------------------------+
| 12/31/2018                                               |
| 01/11/2019                                               |
| 12/03/2050                                               |
+----------------------------------------------------------+
3 rows in set (0.00 sec)

更新於: 2019 年 12 月 13 日

596 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始
廣告