?
Solved

Replace all occurrence in pl sql except the first one

Posted on 2013-11-19
8
Medium Priority
?
453 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
Independent Software Vendors: 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

777 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