Solved

Extract xml value from XML clob field

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bash Script to Analyze Oracle Schemas 11 84
Get the parent node - XMLTYPE 9 56
oracle 11g 23 51
Oracle SQL Select unique values from two columns 4 33
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

867 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

15 Experts available now in Live!

Get 1:1 Help Now