如何从 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 次浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.