如何从 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)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP