0

i would like to do an automatic replace with an oracle query in multiple strings available in an oracle table, by changing fields separator ','between first occurrence of 'select' and first occurrence of 'from' without having impact in case is in place a function:

AS IS:

with result as (select column_a, column_b, count(*)  from (select column1, column2, column3 ..
select column1, count(distinct coalesce(column2, cast('zz' as nvarchar2(2)))) as value from ...
select count(*) from (select ....

EXPECTED:

with result as (select column_a || column_b || count(*) from (select column1, column2, column3 ..
select column1 || count(distinct coalesce(column2, cast('zz' as nvarchar2(2)))) as value from ...
select count(*) from (select column1, column2, column3 ..
2
  • where is all your table DDL, sample data, expected output? Commented Jul 19, 2023 at 21:54
  • Those are the srtings i have in the column (AS IS). I expect that after replace_regexp to have that replace mentioned above (EXPECTED). In that column i store sql quries that i have to to run later. Commented Jul 20, 2023 at 5:58

1 Answer 1

0

How about not even using replace_regexp, just use instr , substr and replace

with dt as (select 
'with result as (select column_a, column_b, count(*)  from (select column1, column2, column3 ..
select column1, count(distinct coalesce(column2, cast("zz" as nvarchar2(2)))) as value from ...
select count(*) from (select ....' as str from dual ),
 temp as ( 
select instr(str,'select'), instr(str,'from'), replace(substr(str,instr(str,'select')+length('select'),instr(str,'from')-instr(str,'select')-length('select')),',' ,' || ') fixed,
substr(str,0,instr(str,'select')+length('select')) astart ,
substr(str,instr(str,'select')+length('select'),instr(str,'from')-instr(str,'select')-length('select')) middle,
substr(str,instr(str,'from')) theend 
 from dt
 )
 select astart || middle || theend as  original, astart || fixed || theend as res
  from temp 

Here is the result, all you'll need later is to write an appropriate update statement.

original 

with result as (select  column_a, column_b, count(*)  from (select column1, column2, column3 ..
select column1, count(distinct coalesce(column2, cast("zz" as nvarchar2(2)))) as value from ...
select count(*) from (select ....

result

with result as (select  column_a ||  column_b ||  count(*)  from (select column1, column2, column3 ..
select column1, count(distinct coalesce(column2, cast("zz" as nvarchar2(2)))) as value from ...
select count(*) from (select ....
Sign up to request clarification or add additional context in comments.

3 Comments

This works only if values are concatenated as you did in the example, but if each query is a different row, in case of count(distinct coalesce(column2, cast("zz" as nvarchar2(2)))) it will also replace separator and is not not correct
@MathewLinton I see what you mean, I didn't notice the last part, that you require the function calls to remain untouched... well then good luck with that
@MathewLinton also each line is a different row with different select , I mistook it for a single large query. May be you should make it more clear in the description for others to understand.

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.