简体   繁体   中英

Extract field from XML Data in SQL

I have an issue as I never done that before.

I have an SQL table with the following :

ID int;
xml_record xml;

The xml record is looking like that :

<root xml:space="preserve" id="XXX">
<c1>Data1</c1>
<c2>Data2</c2>
<c3>Data3</c3>
...
<cn>DataN</cn>
</root>

However I tried to use the following query with no success (return null) :

SELECT xml_record.value('c1[1]','varchar(50)') as value_c1
FROM myTable

The problem might come from the "space" but not sure.

You just need to fix the expression:

SELECT xml_record.value('(/root/c1)[1]','varchar(50)') AS value_c1
FROM ...
   SELECT 
   xml_record.value
   ( '(/root/c1/text())[1])', 
  'varchar(50)') as value_c1
   FROM myTable 

else remove the first xml line

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