Solved

query to parse string

Posted on 2014-01-10
8
423 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
[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
  • 4
  • 3
8 Comments
 
LVL 74

Expert Comment

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

Author Comment

by:gs79
ID: 39772572
Thanks

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

Expert Comment

by:sdstuber
ID: 39772575
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39772592
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
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39772644
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
ID: 39772664
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
ID: 39772736
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
ID: 39772756
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

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!

Question has a verified solution.

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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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