简体   繁体   中英

Convert XML Type to VARCHAR

I am trying to convert the data extracted from XMLTYPE column to varchar but it gives me error:

SELECT TO_CHAR(EXTRACT (XML_DATA, '/root/MYData/text()'))
  FROM TBL_DATA

Gives error "Inconsistent datatypes: expected NUMBER got - "

My motive is to put the extracted value into a variable and use it further in stored proc and the node can contain number or alphabets:

SELECT TO_CHAR(EXTRACT (XML_DATA, '/root/MYData/text()')) INTO TEMP_VAR
  FROM TBL_DATA

Use EXTRACTVALUE rather than EXTRACT :

SQL Fiddle

Oracle 11g R2 Schema Setup :

CREATE TABLE TBL_DATA ( XML_DATA ) AS
SELECT XMLTYPE( '<root><MYData>test string</MYData></root>' ) FROM DUAL;

Query 1 :

SELECT EXTRACTVALUE(XML_DATA, '/root/MYData/text()')
  FROM TBL_DATA

Results :

| EXTRACTVALUE(XML_DATA,'/ROOT/MYDATA/TEXT()') |
|----------------------------------------------|
|                                  test string |

Query 2 :

However, EXTRACT and EXTRACTVALUE are deprecated and oracle recommends using XMLTABLE instead:

SELECT value
FROM   TBL_DATA t
       CROSS JOIN
       XMLTABLE(
         '/root/MYData'
         PASSING t.XML_DATA
         COLUMNS value VARCHAR2(200) PATH 'text()'
       )

Results :

|       VALUE |
|-------------|
| test string |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM