我的字符串数据列格式如下:
Katy Perry Tickets - Staples Center. 09/19/2014 7:30 PM. 209 11
Atlanta Braves Tickets vs Miami Marlins Tickets - Turner Field. 08/31/2014 5:10 PM. 411L 2
我需要从每个字段中提取日期值,并且常见格式是从右边开始直到下一个完整停止。日期总是在这两者之间。
有时会在日期左侧的文本中发生句号,所以我们不能相信。
我如何获得特定的子字符串? SQL大师,你需要的建议......
感谢。
答案 0 :(得分:2)
尝试使用Substring_Index
Select Substring_Index(Substring_Index( txt, ".", -2), ".", 1)
From Tablename
答案 1 :(得分:1)
你可以使用PREG functions for mysql,我已经安装了UDF功能,我得到了这个:
1-创建和插入样本数据:
create table your_table (
txt_col text
);
insert into your_table values
('Katy Perry Tickets - Staples Center. 09/19/2014 7:30 PM. 209 11'),
('Atlanta Braves Tickets vs Miami Marlins Tickets - Turner Field. 08/31/2014 5:10 PM. 411L 2');
2-使用日期模式测试PREG_CAPTURE
函数:
mysql> SELECT PREG_CAPTURE(
'/([0-9]+\\/[0-9]+\\/[0-9]+)\\s[0-9]+:[0-9]+\\s[a-z]+/i' ,
txt_col, 0 ) as date_captured,
txt_col from your_table;
+--------------------+--------------------------------------------------------------------------------------------+
| date_captured | txt_col |
+--------------------+--------------------------------------------------------------------------------------------+
| 09/19/2014 7:30 PM | Katy Perry Tickets - Staples Center. 09/19/2014 7:30 PM. 209 11 |
| 08/31/2014 5:10 PM | Atlanta Braves Tickets vs Miami Marlins Tickets - Turner Field. 08/31/2014 5:10 PM. 411L 2 |
+--------------------+--------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
date_captured
列是提取的文字。