Home > Net >  How do I get a context from field table using regex in oracle SQL?
How do I get a context from field table using regex in oracle SQL?

Time:03-26

How can I catch the part of content "Description scenario::" and the rest of the information using REGEXP_LIKE in SQL Oracle?

select description 
from tbl_error
where REGEXP_LIKE(description, '[Description scenario::].*$' )

Error Details

Report steps Erro: WEB
Contributor name:: Lorem Lorem
Description scenario:: Lorem ipsum dolor sit amet. Est ducimus magnam qui culpa internos in Quis neque et quibusdam ipsa. Aut dolorum omnis qui doloribus eveniet aut neque impedit est expedita consectetur et omnis nulla non sunt sequi. Hic dolor incidunt aut itaque ipsam autem fuga in rerum molestiae. A accusantium facere ad impedit quasi et dolorem quia cum veniam dolores et odit voluptate aut animi illum.

CodePudding user response:

If regex isn't a must, use substr instr combination:

SQL> select substr(description, instr(description, 'Description scenario::')  
  2                             length('Description scenario::')   1
  3               ) as result
  4  from tbl_error;

RESULT
--------------------------------------------------------------------------------
Lorem ipsum dolor sit amet. Est ducimus magnam qui culpa internos in Quis neque
et quibusdam ipsa. Aut dolorum omnis qui doloribus eveniet aut neque impedit est
 expedita consectetur et omnis nulla non sunt sequi. Hic dolor incidunt aut itaq
ue ipsam autem fuga in rerum molestiae. A accusantium facere ad impedit quasi et
 dolorem quia cum veniam dolores et odit voluptate aut animi illum.


SQL>

CodePudding user response:

You do not need (slow) regular expressions and can use simple string functions:

SELECT SUBSTR(
         description,
         INSTR(description, 'Description scenario::')   LENGTH('Description scenario::')
       ) AS description_scenario
FROM   tbl_error;

Which, for the sample data:

CREATE TABLE tbl_error (description) AS
SELECT 'Error Details

Report steps Error:: WEB
Contributor name:: Lorem Lorem
Description scenario:: Lorem ipsum dolor sit amet. Est ducimus magnam qui culpa internos in Quis neque et quibusdam ipsa. Aut dolorum omnis qui doloribus eveniet aut neque impedit est expedita consectetur et omnis nulla non sunt sequi. Hic dolor incidunt aut itaque ipsam autem fuga in rerum molestiae. A accusantium facere ad impedit quasi et dolorem quia cum veniam dolores et odit voluptate aut animi illum.'
FROM DUAL

Outputs:

DESCRIPTION_SCENARIO
Lorem ipsum dolor sit amet. Est ducimus magnam qui culpa internos in Quis neque et quibusdam ipsa. Aut dolorum omnis qui doloribus eveniet aut neque impedit est expedita consectetur et omnis nulla non sunt sequi. Hic dolor incidunt aut itaque ipsam autem fuga in rerum molestiae. A accusantium facere ad impedit quasi et dolorem quia cum veniam dolores et odit voluptate aut animi illum.

If all the values are on individual lines and the separator between the key and the value is :: then you can extract all the key-value pairs (again, without regular expressions) using:

WITH bounds (description, spos, epos) AS (
  SELECT description,
         1,
         INSTR(description, CHR(10), 1)
  FROM   tbl_error
UNION ALL
  SELECT description,
         epos   1,
         INSTR(description, CHR(10), epos   1)
  FROM   bounds
  WHERE  epos > 0
),
lines (line) AS (
  SELECT CASE epos
         WHEN 0
         THEN SUBSTR(description, spos)
         ELSE SUBSTR(description, spos, epos - spos)
         END
  FROM   bounds
),
separators (line, sep_pos) AS (
  SELECT line,
         INSTR(line, '::')
  FROM   lines
)
SELECT SUBSTR(line, 1, sep_pos - 1) AS type,
       LTRIM(SUBSTR(line, sep_pos   2)) AS value
FROM   separators
WHERE  sep_pos > 0

Which, for the sample data, outputs:

TYPE VALUE
Report steps Error WEB
Contributor name Lorem Lorem
Description scenario Lorem ipsum dolor sit amet. Est ducimus magnam qui culpa internos in Quis neque et quibusdam ipsa. Aut dolorum omnis qui doloribus eveniet aut neque impedit est expedita consectetur et omnis nulla non sunt sequi. Hic dolor incidunt aut itaque ipsam autem fuga in rerum molestiae. A accusantium facere ad impedit quasi et dolorem quia cum veniam dolores et odit voluptate aut animi illum.

db<>fiddle here

  • Related