Solved

Replace all occurrence in pl sql except the first one

Posted on 2013-11-19
8
447 Views
Last Modified: 2013-11-19
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

0
Comment
Question by:Swadhin Ray
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 39659138
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

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39659260
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

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39659270
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:sdstuber
ID: 39659284
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.
0
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 39659323
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.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 39659340
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
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39659344
in other words...

remove this portion  (including the comma) from the outermost regexp_replace


,
           REGEXP_INSTR(my_data, rep2) + 1

Open in new window

0
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 39659377
Superb !!!!!!!!............
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
minium over 4 numeric columns for each row in oracle 2 56
pl/sql parameter is null sometimes 2 38
oracle query 3 34
scheduler notification 9 77
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question