Solved

Extract xml value from XML clob field

Posted on 2013-11-21
8
727 Views
Last Modified: 2014-07-23
Hi,
I have a xml clob field 'data_area' in a table named 'xyz'. The value in the clob field 'data_area' looks like this:
<fpiInfo>
<effectiveDate>2013-04-21</effectiveDate>
<sex>M</sex>
<educationLevel>A</educationLevel>
<race>M</race>
<occupation>M</occupation><occupationStatus>M</occupationStatus></fpiInfo>

I want to extract effective date and race from this table 'xyz'.
0
Comment
Question by:baralp
  • 4
8 Comments
 
LVL 31

Expert Comment

by:awking00
ID: 39667152
select
extract(x.data_area,'/fpiInfo/effective_Date/text()').getstringval() as effectiveDate,
extract(x.data_area,'/fpiInfo/race/text()').getstringval() as race
from xyz x;
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39667174
you need to convert the clob into xmltype  using the XMLTYPE() constructor

SELECT EXTRACTVALUE(xmltype(data_area), '//effectiveDate'),
       EXTRACTVALUE(xmltype(data_area), '//race')
  FROM xyz
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39859963
baralp,

Did you try the answers above before closing the question?



The accepted answer should be producing an ora-00932 inconsistent datatypes error.


You can't apply the EXTRACT function to a text field (clob/varcahr2)

it only works on XMLTYPE and there isn't an implicit conversion from text types to xmltype
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
ID: 39859992
also,  even if you do the xmltype conversion yourself,  the accepted answer still doesn't produce the correct results

It will return null for the efffectivedate rather than the expected 2013-04-21
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39860018
here's a simple test case illustrating the problems described above...

SQL> column effectivedate format a20
SQL> column race format a4
SQL> WITH xyz
  2       AS (SELECT '<fpiInfo>
  3  <effectiveDate>2013-04-21</effectiveDate>
  4  <sex>M</sex>
  5  <educationLevel>A</educationLevel>
  6  <race>M</race>
  7  <occupation>M</occupation><occupationStatus>M</occupationStatus></fpiInfo>'
  8                      data_area
  9             FROM DUAL)
 10  SELECT EXTRACT(x.data_area, '/fpiInfo/effective_Date/text()').getstringval() AS effectivedate,
 11         EXTRACT(x.data_area, '/fpiInfo/race/text()').getstringval() AS race
 12    FROM xyz x;
       EXTRACT(x.data_area, '/fpiInfo/race/text()').getstringval() AS race
       *
ERROR at line 11:
ORA-00932: inconsistent datatypes: expected - got -


SQL> WITH xyz
  2       AS (SELECT '<fpiInfo>
  3  <effectiveDate>2013-04-21</effectiveDate>
  4  <sex>M</sex>
  5  <educationLevel>A</educationLevel>
  6  <race>M</race>
  7  <occupation>M</occupation><occupationStatus>M</occupationStatus></fpiInfo>'
  8                      data_area
  9             FROM DUAL)
 10  SELECT EXTRACT(xmltype(x.data_area), '/fpiInfo/effective_Date/text()').getstringval()
 11             AS effectivedate,
 12         EXTRACT(xmltype(x.data_area), '/fpiInfo/race/text()').getstringval() AS race
 13    FROM xyz x;

EFFECTIVEDATE        RACE
-------------------- ----
                     M

SQL> WITH xyz
  2       AS (SELECT '<fpiInfo>
  3  <effectiveDate>2013-04-21</effectiveDate>
  4  <sex>M</sex>
  5  <educationLevel>A</educationLevel>
  6  <race>M</race>
  7  <occupation>M</occupation><occupationStatus>M</occupationStatus></fpiInfo>'
  8                      data_area
  9             FROM DUAL)
 10  SELECT EXTRACTVALUE(xmltype(data_area), '//effectiveDate') effectivedate,
 11         EXTRACTVALUE(xmltype(data_area), '//race') race
 12    FROM xyz;

EFFECTIVEDATE        RACE
-------------------- ----
2013-04-21           M

SQL>

Open in new window



Note you can fix the code of the 1st post like this...

SELECT EXTRACT(xmltype(x.data_area), '/fpiInfo/effectiveDate/text()').getstringval()
           AS effectivedate,
       EXTRACT(xmltype(x.data_area), '/fpiInfo/race/text()').getstringval() AS race
  FROM xyz x;

but it's a more complicated syntax than what is in the 2nd post
0
 

Author Comment

by:baralp
ID: 40215723
Admin,
Please unlock my ability to ask new question as it is already answered and resolved.

Thanks,
Paras
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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

706 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

17 Experts available now in Live!

Get 1:1 Help Now