Avatar of Gary Antonellis
Gary Antonellis
Flag for United States of America asked on

Parse data from XML node in Oracle table

I have a field in Oracle table with XML data that varies in size and position.  I need to pull out the data from some of these nodes using SQL.

For example in the XML below the node &BADGE_NUM contains the value TEST05.  I would like to write an sql that returns TEST05 in a specific column.  I cannot use SUBSTR because fields prior to BADGE_NUM vary in length so BADGE_NUM is not always in the same position.



dialogresponse.xml?GID=1&DID=11&EVENT_NAME=PRDORD-MTN&MODE=ON&MENU_TYPE=PROMPT&XMLINTERF=Y&BADGE_NUM=TEST05&F_CLOCKTIM=20140414101758&F_ENTITY=10393002&F_OPER=1506&F_QTYCOMP=0&STARTSTOP=S&SBM=M&LAB-T=L&F_SUBCODE3=A&F_SUBCODE4=&CREATOR=MBRESNAH&ORIGREF=&F_ENTITY_T=W&trans_reason=SOLUMINA ADJUSTMENT&F_ROUNDASCLOCKOUT=false&
 Unknown error

Thanks!
Oracle Database

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon
PortletPaul

that data looks more like a URL (string) so you might be able to approach it like this
    CREATE TABLE TABLE1
    	("ASTRING" varchar2(500))
    ;
    
    INSERT ALL 
    	INTO TABLE1 ("ASTRING")
    		 VALUES ('dialogresponse.xml?GID=1&DID=11&EVENT_NAME=PRDORD-MTN&MODE=ON&MENU_TYPE=PROMPT&XMLINTERF=Y&BADGE_NUM=TEST05&F_CLOCKTIM=20140414101758&F_ENTITY=10393002&F_OPER=1506&F_QTYCOMP=0&STARTSTOP=S&SBM=M&LAB-T=L&F_SUBCODE3=A&F_SUBCODE4=&CREATOR=MBRESNAH&ORIGREF=&F_ENTITY_T=W&trans_reason=SOLUMINA ADJUSTMENT&F_ROUNDASCLOCKOUT=false& Unknown error')
    SELECT * FROM dual
    ;

**Query 1**:

    SELECT
    
        CASE WHEN posright > posleft THEN
                substr(astring, posleft, posright-posleft)
             ELSE NULL
        END as partstring
    
    FROM (
          SELECT
              instr(astring,'=',instr(astring,'BADGE_NUM') ) + 1 AS posleft
            , instr(astring,'&',instr(astring,'BADGE_NUM') )     AS posright
            , astring
          FROM table1
         )
    	
    	
    

**[Results][2]**:
    
    | PARTSTRING |
    |------------|
    |     TEST05 |



  [1]: http://sqlfiddle.com/#!4/cd542/7

Open in new window

nb It uses a subquery to allow posleft and posright to be referenced which also has the advantage of allowing use of a case expression to assist in validation.
ASKER CERTIFIED SOLUTION
Sean Stuber

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gary Antonellis

ASKER
This worked perfectly in Toad but when I moved to clients network and run with SQL Developer it interpreted the ']' as a variable, so it prompted me to enter a value.  

How can I get this to work in SQL Developer?
PortletPaul

try mine :)
Your help has saved me hundreds of hours of internet surfing.
fblack61