MySQL 如何从字符串中提取日期

MySQL 如何从字符串中提取日期

要从MySQL的字符串提取日期,请使用SUBSTRING_INDEX()。让我们首先创建一个表 −

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

使用insert命令向表中插入一些记录 −

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)

使用select语句显示表中的所有记录 −

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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程