Solved

Replace all occurrence in pl sql except the first one

Posted on 2013-11-19
8
427 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
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
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
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 16

Author Comment

by:Swadhin Ray
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now