Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle SQL: Regex for finding out values from a Stringified map of fixed format

I have a String in a specific column of my table which is of a certain fixed format.

For ex:

Score breakup: {TR_SCORE=0, SAR_SCORE=2, LO_SCORE=2, CASH_SCORE=1, TPP_SCORE=1, CREDITS_SCORE=1}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I wish to write a regex to separate out all the scoring values as below:

SELECT TR_SCORE, SAR_SCORE, LO_SCORE 
  FROM some_table 
 WHERE message LIKE 'Score breakup:%';
TR_SCORE SAR_SCORE LO_SCORE
0 2 3
1 3 0

Can anyone help create a regex for the same?

>Solution :

You can use REGEXP_REPLACE() function along with \d+ pattern in order to extract the substrings with digits only such as

SELECT col,
       REGEXP_REPLACE(col,'(.*TR_SCORE=)(\d+)(.*)','\2') AS tr_score,
       REGEXP_REPLACE(col,'(.*SAR_SCORE=)(\d+)(.*)','\2') AS sar_score,
       REGEXP_REPLACE(col,'(.*LO_SCORE=)(\d+)(.*)','\2') AS lo_score
  FROM t

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading