Solved

query to parse string

Posted on 2014-01-10
8
416 Views
Last Modified: 2014-01-10
I have a table with two columns..

I am just displaying one record for brevity

COL_STR                                                  KEY
COL_A, COL_B, COL_C, COL_D                  COL_A

I need a query to process the above two values and return the following

return_val

TGT.COL_B = SRC.COLB
TGT.COL_C = SRC.COL_C
TGT.COL_D = SRC.COL_D

i would like the value in the key column to be excluded (in the above example COL_A)

if key has following
COL_A, COL_B

the output should be:

TGT.COL_C = SRC.COL_C
TGT.COL_D = SRC.COL_D

Please let me know if this is possible..

Thanks,
0
Comment
Question by:gs79
  • 4
  • 3
8 Comments
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
what version of the db?  Full 4-digit version please  like 11.2.0.1   not 11g
0
 

Author Comment

by:gs79
Comment Utility
Thanks

Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
This should work in 10.2.0.1 and above
I made the assumption that you would have 10 columns or less.  If that's not true, simply bump up the LEVEL <= 10 to something large enough to accomodate your data


SELECT 'TGT.' || str || ' = ' || 'SRC.' || str
  FROM (SELECT REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   str
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL
        MINUS
        SELECT REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL)
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
Assuming there is an id column or some other values in your table that you want to keep track of with each row simply add it to the selects...

For example, using an ID column...

SELECT id, 'TGT.' || str || ' = ' || 'SRC.' || str
  FROM (SELECT id,
               REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   str
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   col_str,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL
        MINUS
        SELECT id,
               REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
          FROM yourtable,
               (    SELECT LEVEL n
                      FROM DUAL
                CONNECT BY LEVEL <= 10)
         WHERE REGEXP_SUBSTR(
                   key,
                   '[^ ,]+',
                   1,
                   n
               )
                   IS NOT NULL)
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 13

Expert Comment

by:Alexander Eßer [Alex140181]
Comment Utility
You're always doing a great job here at EE, sdstuber. But I think it would be nice, if you might explain your (awesome) SQL statements a bit more to the asker (so they may learn better to use and adept).
Just a thought ;-) Keep going!

Kind regards,
Alex
0
 

Author Comment

by:gs79
Comment Utility
Thanks sdstuber..It works..but I need to do this procedurally in a dynamic sql and generate a merge statement..i will close this and open and another question with more details

Thanks
0
 

Author Comment

by:gs79
Comment Utility
Sdstuber..Is there a way to get result it as one value rather than multiple records

I just want the result as a value not the records, the above example gives me 3 records instead of record with:
TGT.COL_B = SRC.COLB
TGT.COL_C = SRC.COL_C
TGT.COL_D = SRC.COL_D

Thanks
0
 

Author Comment

by:gs79
Comment Utility
As I have closed this..I have opened a new one..

http://www.experts-exchange.com/Database/Oracle/Q_28335896.html

Thanks
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

Title # Comments Views Activity
Sql to get orphans 7 50
null value 14 57
Migrating an SQL 2008 database to Oracle 12c 3 59
Need a replacement data type in Oracle 6 24
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 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.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

771 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

7 Experts available now in Live!

Get 1:1 Help Now