SQL:格式的最后一次出现

时间:2017-12-11 22:40:27

标签: sql regex oracle substr

我有一个(oracle)文本字段,如下所示。它是故障单的摘要记录信息,其中包括相关的用户代码,日期和时间以及一些注释。如你所见,我在这个领域有日期,但是作为一个字符串。 我想提取最后一次出现的日期格式,在这种情况下' 2015-09-07'。或者更好的是,如果我可以在评论为“Ticket Fixed'或者'票务已关闭'。

{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

我尝试使用SUBSTR,但这不起作用,因为最后一条评论的内容/长度可能会发生变化。

使用Oracle数据库。

1 个答案:

答案 0 :(得分:0)

SQL Fiddle

Oracle 11g R2架构设置

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' )

查询1

|  ID |                 TIME | TICKET_COMMENT |
|-----|----------------------|----------------|
| MFR | 2015-09-07T13:37:00Z |   Ticket fixed |

<强> Results

class Event(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    calendar = models.ForeignKey(Calender, on_delete=models.CASCADE)
    name = models.CharField(max_length=30, default='Event')
    start_date = models.DateTimeField(default='1950-01-01')
    end_date = models.DateTimeField(default='1950-01-01')
    email = models.CharField(max_length=40, default='example@example.com')
    location = models.CharField(max_length=40, default='location')
    notes = models.CharField(max_length=200, default='note')
    repeat = models.IntegerField()
    # 0 = no repeat
    # 1 = daily
    # 2 = weekly
    # monthly
    created = models.DateTimeField(auto_now_add=True)