Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Extract xml value from XML clob field

Posted on 2013-11-21
8
Medium Priority
?
841 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
[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
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 74

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 74

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 74

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 74

Accepted Solution

by:
sdstuber earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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

650 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