Avatar of Swadhin Ray
Swadhin Ray
Flag for United States of America asked on

Replace all occurrence in pl sql except the first one

Hello Experts,

I have the below SQL statement:

SELECT to_clob('{
      "metadata": {
                "VISIT_TYPE":"TEST"
      },
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V12-4002-2013-11"
    }
       ]
        ,
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V284-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V12-5302-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V288-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V95-5293-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V292-5287-2013-11"
    }
       ]
        } ' ) my_data
    FROM dual;

Open in new window



I want to replace all occurrence of the below with a comma except the first one:

] , "TODAY":[

Open in new window

] , "UPCOMING":[

Open in new window

Oracle Database

Avatar of undefined
Last Comment
Swadhin Ray

8/22/2022 - Mon
Swadhin Ray

ASKER
This is how I was trying but it replace all occurrence the of TODAY :

create table t_clob (col1 clob );

DECLARE

        l_replace_today VARCHAR2(1000) := ']
        ,
      "TODAY":[';

        l_clob CLOB;

BEGIN

        SELECT to_clob('{
      "metadata": {
                "VISIT_TYPE":"TEST"
      },
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V12-4002-2013-11"
    }
       ]
        ,
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V284-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V12-5302-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V288-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V95-5293-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V292-5287-2013-11"
    }
       ]
        }')
          INTO l_clob
          FROM dual;

        l_clob := REPLACE(l_clob
                         ,l_replace_today
                         ,',');
                         
 insert into t_clob values(  l_clob);
 commit;                      

END;

Open in new window

Sean Stuber

this does what you asked, but I don't think it does what you want.

SELECT REGEXP_REPLACE(
           my_data,
           rep2,
           ',',
           REGEXP_INSTR(my_data, rep2) + 1
       )
           my_data
  FROM (SELECT REGEXP_REPLACE(
                   my_data,
                   rep1,
                   ',',
                   REGEXP_INSTR(my_data, rep1) + 1
               )
                   my_data,
               rep2
          FROM (SELECT TO_CLOB('{
      "metadata": {
                "VISIT_TYPE":"TEST"
      },
      "UPCOMING":[

          {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V12-4002-2013-11"
    }
       ]
        ,
      "UPCOMING":[

          {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V284-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

          {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V12-5302-2013-11"
    }
       ]
        ,
      "TODAY":[

          {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V288-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

          {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V95-5293-2013-11"
    }
       ]
        ,
      "TODAY":[

          {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V292-5287-2013-11"
    }
       ]
        } ') my_data,
                       ']\s*,\s*"TODAY":\[' rep1,
                       ']\s*,\s*"UPCOMING":\[' rep2
                  FROM DUAL))

Open in new window

Sean Stuber

the reason I say that is you have UPCOMING nodes both with and without preceding close brackets "]"

So, searching for ],"UPCOMING"[  lets the first one slip through.

It is however, what you asked for.  Easy fix is to simply replace all of these:   ],"UPCOMING"[   and let the unprefixed one come through.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Sean Stuber

Since this will likely require a followup question...

In that one:  Post your starting text and the final text you want.

Descriptions can be miswritten and misread.  

Given input and expected output removes ambiguity.
Swadhin Ray

ASKER
I was trying the SQL but its works for Today only.

So here is my input:

{
      "metadata": {
                "VISIT_TYPE":"TEST"
      },
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V12-4002-2013-11"
    }
       ]
        ,
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V284-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V12-5302-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V288-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V95-5293-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V292-5287-2013-11"
    }
       ]
        } 

Open in new window


And expected result will be after replacing today and upcoming ..

{
      "metadata": {
                "VISIT_TYPE":"TEST"
      },
      "UPCOMING":[

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V12-4002-2013-11"
    }
       ,

		  {
        "REPORT_TYPE":"UPCOMING",
"VISIT_STATUS":"Pre-Audit - Audit Planned",
"VISIT_STATUS_ID":"1",
"VISIT_ID":"V284-5287-2013-11"
    }
       ]
        ,
      "TODAY":[

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V12-5302-2013-11"
    }
      ,

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V288-5287-2013-11"
    }
      ,

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V95-5293-2013-11"
    }
      ,

		  {
        "REPORT_TYPE":"TODAY",
"VISIT_STATUS":"Audit In Progress",
"VISIT_STATUS_ID":"4",
"VISIT_ID":"V292-5287-2013-11"
    }
       ]
        } 

Open in new window


The way i was looking it to do using a PLSQL block because I am not sure how may records can come under today , upcoming and final.

So there was only three status.
Sean Stuber

the problem isn't sql vs pl/sql, the problem was you were trying to use REPLACE which isn't sophisticated enough to detect white space variations or do positional work

So, like I said above, simply replace all of the  ],"UPCOMING"[

and that will leave the first "UPCOMING":[  in place

notice the first one doesn't have " ]' "  in front of it
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Sean Stuber

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Swadhin Ray

ASKER
Superb !!!!!!!!............