1

I'm new to oracle,i have XML column in table,in that column data is in XML format

i.e,

<?xml version="1.0" encoding="UTF-8"?>
<charge textid="dacebab6-962e-3-b23d70eef85a" id="parse"><properties/></charge>

Now i want get only textid, so how to get textid thorough SQL query.

i tried select XMLSequence(Extract(x,'/charge/textid/text()')) from emp_datails; but not working.

16
  • Please specify what you mean by "not working". Include a stack trace and error message(s). Commented Mar 31, 2016 at 6:07
  • @Jim Garrison: it display ora-06553 pls-306 wrong number or types of arguments in call to 'ogc_x' Commented Mar 31, 2016 at 6:44
  • Possible duplicate of ORACLE 12c XML: Extract value from XML string using EXTRACTVALUE Commented Mar 31, 2016 at 9:47
  • @Florin Ghita: my question is different. how to decide it is duplicate,first resolve my issue after i will agree. Commented Mar 31, 2016 at 11:01
  • Noel's example seems to work. what version of Oracle do you have? Commented Mar 31, 2016 at 11:21

2 Answers 2

2

Use EXTRACTVALUE.

select EXTRACTVALUE(x,'/charge/@textid') from emp_datails;

Example:

WITH x(y) AS
  (SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?>
<charge textid="dacebab6-962e-3-b23d70eef85a" id="parse">asdd<properties/>asd</charge>')
  FROM dual
  )
SELECT EXTRACTVALUE(y, '/charge/@textid') textid FROM x;

TEXTID
----------
dacebab6-962e-3-b23d70eef85a
Sign up to request clarification or add additional context in comments.

1 Comment

how to execute your query, i mean i tried WITH x(y) AS (SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?> <charge textid="dacebab6-962e-3-b23d70eef85a" id="parse">asdd<properties/>asd</charge>') FROM dual ) but it's not execute
0

with this statement you can extract the textid:

-- Your data
with data as
 (select '<?xml version="1.0" encoding="UTF-8"?>
          <charge textid="dacebab6-962e-3-b23d70eef85a" id="parse"><properties/></charge>' as xmlval
    from dual)

 (SELECT attr_value
    FROM data d,
         xmltable('.' PASSING xmltype(d.xmlval) COLUMNS attr_value
                  VARCHAR2(30) PATH '/charge/@textid'));

6 Comments

i tried you query with data as (select '<?xml version="1.0" encoding="UTF-8"?> <charge textid="dacebab6-962e-3-b23d70eef85a" id="parse"><properties/></charge>' as xmlval from dual) but not execute in my scratch pad
what is meaning of with data as?
try with data as (select 'hello' from dual) (select * from data) to understand it. In this case it's only a statement to prepare test data, replace it by your table.
you have to call the whole statement with data as (select '<?xml version="1.0" encoding="UTF-8"?> <charge textid="dacebab6-962e-3-b23d70eef85a" id="parse"><properties/></charge>' as xmlval from dual) (SELECT attr_value FROM data d, xmltable('.' PASSING xmltype(d.xmlval) COLUMNS attr_value VARCHAR2(30) PATH '/charge/@textid'));
:oracle 10g. I tried your query it is executed successfully but out put is not displaying. only it displaying The SQL has been executed successfully.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.