Question

I need to parse a text field (Description), delimited by '\n', into three separate fields. I am doing this by utilizing substr and instr, but it results in difficult to read and repetitive sql. Is there a way to create and use a variable or expression to hold the "position" value returned by the instring function so I can pass that variable to substr instead? My code posted below functions and returns the correct results, but it doesn't feel right. There's a lot of duplication.

Relevant Raw Data:

DBKEY       DBTIME                      DBUSER     DESCRIPTION
40846809    2013-11-18 11:04:11.0000000 abc$userid  The following Message List entry has been logged:\nError Number:5011\nDescription:Planogram: 60E90001006.0SMA :: UPC:  is not numeric\nSeverity:0
40846810    2013-11-18 11:04:11.0000000 abc$userid  The following Message List entry has been logged:\nError Number:5000\nDescription:Planogram: 60E90001006.0SMA :: ID: NEW  not 9 digits\nSeverity:0
40846811    2013-11-18 11:04:11.0000000 abc$userid  The following Message List entry has been logged:\nError Number:5001\nDescription:Planogram: 60E90001006.0SMA :: ID: NEW  not numeric\nSeverity:0

Desired Results:

DBKEY       DBTIME                      USERID  ERROR_NUM   DESCRIPTION SEVERITY
40846809    2013-11-18 11:04:11.0000000 userid  5011        Planogram: 60E90001006.0SMA :: UPC:  is not numeric 0
40846810    2013-11-18 11:04:11.0000000 userid  5000        Planogram: 60E90001006.0SMA :: ID: NEW  not 9 digits    0
40846811    2013-11-18 11:04:11.0000000 userid  5001        Planogram: 60E90001006.0SMA :: ID: NEW  not numeric 0
40846812    2013-11-18 11:04:11.0000000 userid  5003        Planogram: 60E90001006.0SMA :: ID: NEW  ID must begin with 000,200,220,900,950,990,or 999   0

Current Code:

SELECT DBKEY,DBTIME,
        SUBSTR(DBUSER,INSTR(DBUSER,'$',1,1)+1) AS USERID,
        SUBSTR(ERROR_NUM,INSTR(ERROR_NUM,':')+1) AS ERROR_NUM,
        SUBSTR(DESC1,INSTR(DESC1,':')+1) AS DESCRIPTION,
        SUBSTR(SEVERITY,INSTR(SEVERITY,':')+1) AS SEVERITY
FROM(
    SELECT l.DBKEY,DBTIME,DBUSER,
        --substring(description,first+2,second-first-2)
        SUBSTR(DESCRIPTION,INSTR(DESCRIPTION,'\n',1,1)+2,INSTR(DESCRIPTION,'\n',1,2)-INSTR(DESCRIPTION,'\n',1,1)-2) AS ERROR_NUM,
        --substring(description,second+2,third-second-2)
        SUBSTR(DESCRIPTION,INSTR(DESCRIPTION,'\n',1,2)+2,INSTR(DESCRIPTION,'\n',1,3)-INSTR(DESCRIPTION,'\n',1,2)-2) AS DESC1,
        --substring(description,third+2) 
        SUBSTR(DESCRIPTION,INSTR(DESCRIPTION,'\n',1,3)+2) AS SEVERITY
        /*,
        INSTR(DESCRIPTION,''\n'',1,1) as first,
        INSTR(DESCRIPTION,''\n'',1,2) as second,
        INSTR(DESCRIPTION,''\n'',1,3) as third,
        */
    FROM EVENT_LOG l
)derivedtbl
Was it helpful?

Solution

I suggest using REGEXP_SUBSTR which is a great function to achieve your expected results:

SELECT
    l.DBKEY,
    l.DBTIME,
    REGEXP_SUBSTR(l.DBUSER, '[^$]+$')                 AS USERID,
    REGEXP_SUBSTR(l.DESCRIPTION, '[0-9]{4}')          AS ERROR_NUM,
    REPLACE(REGEXP_SUBSTR(l.DESCRIPTION, 'Planogram:[^\]+\\n'), '\n', '')
                                                      AS DESCRIPTION,
    REGEXP_SUBSTR(l.DESCRIPTION, '\d+$')              AS SEVERITY
FROM
    EVENT_LOG l;

I have created and tested a SQLFiddle. For more information about REGEXP_SUBSTR you may want to read the Oracle Docs.

OTHER TIPS

regexp_replace will do it:

select 
  dbkey,
  dbtime,
  REGEXP_REPLACE(dbuser,'^.*\$','',1,1) dbuser,
  REGEXP_REPLACE(description, '.*Error Number:([0-9]+).*','\1',1,1) error_num,
  REGEXP_REPLACE(description, '.*Description:(.+)\\n.*','\1',1,1) description,
  REGEXP_REPLACE(description, '.*Severity:(.+)','\1',1,1) severity
from rawdata;

The results

DBKEY       DBTIME                      DBUSER  ERROR_NUM  DESCRIPTION                                         SEVERITY
40846809    2013-11-18 11:04:11.0000000 userid  5011       Planogram: 60E90001006.0SMA :: UPC: is not numeric       0
40846810    2013-11-18 11:04:11.0000000 userid  5000       Planogram: 60E90001006.0SMA :: ID: NEW not 9 digits      0
40846811    2013-11-18 11:04:11.0000000 userid  5001       Planogram: 60E90001006.0SMA :: ID: NEW not numeric       0

SQLFiddle here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top