首页 文章资讯内容详情

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

2026-06-05 1 花语

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

create table DemoTable -> ( -> Title text -> );

使用插入命令在表中插入一些记录-

insert into DemoTable values(John has got joining date.12/31/2018); insert into DemoTable values(Carol has got joining date.01/11/2019); insert into DemoTable values(Sam will arrive at.12/03/2050);

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

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中的字符串中提取日期的查询-

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)