• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

query to parse string

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
gs79
Asked:
gs79
  • 4
  • 3
1 Solution
 
sdstuberCommented:
what version of the db?  Full 4-digit version please  like 11.2.0.1   not 11g
0
 
gs79Author Commented:
Thanks

Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
0
 
sdstuberCommented:
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
sdstuberCommented:
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
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
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
 
gs79Author Commented:
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
 
gs79Author Commented:
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
 
gs79Author Commented:
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now