Solved

Replace all occurrence in pl sql except the first one

Posted on 2013-11-19
8
442 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

830 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