0

Just a quick check, is it possible to replace all the occurrences with a single select statement. If not, we plan to write a function to do this replace.

Sample string

select '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@' str from dual

Expected output

str
---
col1 val1, col2 val2, col3 val3, col4 val4, col5 val5

Code so far

with test_table as 
(Select '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@' str from dual)

   select level "occurrence", REGEXP_substr(str, '#(.*?):', 1, level, 'in', 1) "column", REGEXP_substr(str, ':(.*?)@', 1, level, 'in', 1) "value"
   from test_table
   CONNECT BY REGEXP_COUNT(str, '#.+?:.+?@') >= level

Additional Details

  1. String pattern is fixed and known beforehand.
  2. Occurrence of the given pattern is dynamic, may have any number of [column:value] pairs - they are actually [table column:alias] set.
  3. I have trimmed the sample string for simplicity, however it contains additional details (table joins, where clause etc.), so we would need to replace all the occurrences. It’s actually a select statement.

2 Answers 2

3

Query:

SELECT REGEXP_REPLACE(
         '#col1:val1@, #col2:val2@, #col3:val3@, #col4:val4@, #col5:val5@',
         '#(.*?):(.*?)@',
         '\1 \2'
       ) AS str
FROM   DUAL

Output:

STR
-----------------------------------------------------
col1 val1, col2 val2, col3 val3, col4 val4, col5 val5
Sign up to request clarification or add additional context in comments.

Comments

0

My try:

with test_table as 
(Select '#col1:val1@, #col2:val2@, #col3:val3@,     #col4:val4@, #col5:val5@' str 
 from dual)

select regexp_replace(regexp_replace(str, '[#@]', ''), '[ :]+', ' ') rez 
from test_table
;

The inner regexp_replace replaces the # and @ with nothing(empty string) and the outer regexp replaces the : and spaces with one space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.