Solved

Extract xml value from XML clob field

Posted on 2013-11-21
8
765 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 32

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.

Question has a verified solution.

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

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

776 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