MartinusP
MartinusP

Reputation: 41

how to use the REGEXP_SUBSTR function to extract a substring from a string?

I need to extract the word and the sequence of words from the error log.

below log examples:

2019.06.08 14:32:36 ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] [Teradata DBMS] : No more spool space in aload50.
2019.06.08 14:32:36 ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] [Teradata DBMS] : No more spool space in aload50. (ef)
2019.06.08 14:32:36 ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] [Teradata DBMS] : No more spool space in dload50.
2019.06.08 14:32:36 ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] [Teradata DBMS] : No more spool space in dload50. (ef)
message=[NCR] [Teradata DBMS] : No more spool space in aload50. (ef)
message=[NCR] [Teradata DBMS] : No more spool space in dload50. (ef)
message=[NCR] [Teradata DBMS] : No more spool space in aload50. (ee)
message=[NCR] [Teradata DBMS] : No more spool space in dload50. (ee)

I need to extract substring:

error_log:

[Teradata DBMS] : No more spool space in aload50.

without (eg)

and username: for example:

aload50

username can be:

aload01 to aload999

and

dload01 to dload999

select 
REGEXP_SUBSTR('2019.06.08 14:32:36  ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] error_message[Teradata DBMS] : No more spool space in aload50.',' regexp_for_error_log') AS error_log,
REGEXP_SUBSTR('2019.06.08 14:32:36  ERR 10298587    2019-06-07  PROJECT_NAME    script.sql  4483    2646 HY000 [NCR] [Teradata DBMS] : No more spool space in aload50.',' regexp_for_user_name') AS user_name,
FROM DUAL;

Upvotes: 0

Views: 477

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521409

We can try using REGEXP_REPLACE here with a capture group:

SELECT
    REGEXP_REPLACE(log, '.*(\[Teradata DBMS\] : .* [^.]+)\..*', '\1') AS error_log,
    REGEXP_REPLACE(log, '.*\[Teradata DBMS\] : .* ([^.]+)\..*', '\1') AS user_name
FROM yourTable;

Demo

Upvotes: 2

Related Questions