?
Solved

Extract xml value from XML clob field

Posted on 2013-11-21
8
Medium Priority
?
833 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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
Suggested Courses

765 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