i有一个(oracle)文本字段,如下所示。这是机票的摘要记录信息,其中包括相关的用户代码,日期和时间,然后进行一些评论。如您所见,我在此字段中有日期,但作为字符串。在这种情况下,我想拿出日期格式的最后一次发生,即" 2015-09-07"。如果我可以拿出评论"固定票"或"门票关闭"的日期,甚至更好。
{ABC, 2015-08-31 10:27}: Ticket Issued 151553
{ANN, 2015-09-07 13:34}: Assigned to user 'FAR'
{MJJ, 2015-09-07 13:37}: Comments added
{MFR, 2015-09-07 13:37}: Ticket fixed
我试图使用subtr,但这是不起作用的,因为上一条评论的内容/长度可能会改变。
使用Oracle数据库。
sql小提琴
Oracle 11G R2架构设置:
CREATE TABLE tickets( details ) AS
SELECT '{ABC, 2015-08-31 10:27}: Ticket Issued 151553' FROM DUAL UNION ALL
SELECT '{ANN, 2015-09-07 13:34}: Assigned to user ''FAR''' FROM DUAL UNION ALL
SELECT '{MJJ, 2015-09-07 13:37}: Comments added' FROM DUAL UNION ALL
SELECT '{MFR, 2015-09-07 13:37}: Ticket fixed' FROM DUAL;
查询1 :
SELECT *
FROM (
SELECT SUBSTR( details, 2,3 ) AS id,
TO_DATE( SUBSTR( details, 7,16 ), 'YYYY-MM-DD HH24:MI' ) AS time,
SUBSTR( details, 26 ) AS ticket_comment
FROM tickets
)
WHERE ticket_comment IN ( 'Ticket fixed', 'Ticket Closed' )
结果:
| ID | TIME | TICKET_COMMENT |
|-----|----------------------|----------------|
| MFR | 2015-09-07T13:37:00Z | Ticket fixed |