Solved

How get this query ?

Posted on 2014-07-23
7
330 Views
Last Modified: 2014-07-23
Hi expert,

below i have query
(SELECT regexp_replace('{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}','^[{"]+|[}"]+$') AS rec
FROM dual
);

Open in new window


result:Result:1":1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":
Expected Result: 1":{"ACC":1,"YBT":"damby}damby
                            2":{"ACC":1,"YBT":"damby}damby
                          3":{"ACC":1,"YBT":


Regards
Thomos
0
Comment
Question by:deve_thomos
  • 4
  • 3
7 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
please as I explained in your other question:

Plase don't post a query that doesn't work and then just assume I'll know what you want it do.

Give me INPUT DATA  (a query is not data)
and expected OUTPUT DATA

along with the parsing rules.

For example of the types of rules I might need:  can you have more than 3 substrings?  How are substrings identified?  Do you need multiple depth searching?

I'm going to assume from your previous question and your query that you want to do {} and " stripping at the outermost level but you haven't specified it here.  

When you post a query and then ask how to get "the query"  you're not giving good enough information.
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
here's an example of what I would expect...

INPUT:  '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}'

Parsing Rule1:  The data is json structured
Parsing Rule 2: Pull each json node out of the string that starts with a { and numeric label "1", "2", "3" and ends with }}
Parsing Rule 3: Return each output as a distinct row with leading and trailing {} and " removed

OUTPUT of 3 rows
1":{"ACC":1,"YBT":"damby}damby
2":{"ACC":1,"YBT":"damby}damby
3":{"ACC":1,"YBT":


Using this example, I would write the query like this...

SELECT REGEXP_REPLACE(
           REGEXP_SUBSTR(
               '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}',
               '"[0-9]+".+?}}',
               1,
               LEVEL
           ),
           '^[{"]+|[}"]+$'
       )
           AS rec
  FROM DUAL
CONNECT BY LEVEL <= 3;

Open in new window


That produces the expected results

HOWEVER - since I'm guessing at your parsing rules and structures of your input it doesn't mean it's correct as a general solution, it will only work for this particular string and possibly others with minor changes.
0
 

Author Comment

by:deve_thomos
Comment Utility
Hi sdstuber,
in this case   you are giving  CONNECT BY LEVEL <= 3;  suppose  my out put is having  10 rows  or more than that  how i will handle that .

Regrads
Thomos
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 73

Expert Comment

by:sdstuber
Comment Utility
assuming the 10 rows have the same parsing rules that I guessed at then

 CONNECT BY LEVEL <= 3;

becomes

 CONNECT BY LEVEL <= 10;

If the parsing rules are different than my guess then you'll have to do something else but I can't begin to guess what.  

I really am guessing - you haven't described at all what your parsing rules are or confirmed the correct input.

I ask for the same things repeatedly (input, parsing rules, expected output) because it's important.  Trusting me to guess correctly is not a reliable methodology for you.
0
 

Author Comment

by:deve_thomos
Comment Utility
Hi Sdstuber,
I am parsing some json data , so it can be  one row or it can more than one  row. exact row we cannot say .
in this case  i cannot fix that  CONNECT BY LEVEL <= 10 or CONNECT BY LEVEL <= 3; Then how i will handle this things

if you want  more information  please let me know.

Regards
Thomos
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
again - assuming MY parsing rules guesses are correct (you haven't confirmed or described your rules) AND the structure of the data will match those rules every time then try something like this.


SELECT *
  FROM (SELECT REGEXP_REPLACE(
                   REGEXP_SUBSTR(
                       '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}',
                       '"[0-9]+".+?}}',
                       1,
                       LEVEL
                   ),
                   '^[{"]+|[}"]+$'
               )
                   AS rec
          FROM DUAL
        CONNECT BY LEVEL <= 10)
 WHERE rec IS NOT NULL;


If 10 isn't sufficient for all cases, make it something larger.
Given the 4k limit on varchar2 fields (at least prior to 12c) and given the structure of the json nodes,  it would be impossible for the value to need to be more than 500, but for efficiency you should pick something smaller but still big enough.
0
 

Author Closing Comment

by:deve_thomos
Comment Utility
Thank u a lot....
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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
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…
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 Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

763 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

8 Experts available now in Live!

Get 1:1 Help Now