简体   繁体   中英

Extracting a value from a XML column in SQL Server 2012

I have seen many questions addressing this issue, but unfortunately I still was not able to make it work.

Here's an example of the XML data contained in an XML column called RentalValueAmount in a table called Units :

<X C="1" I="0">
  <E D="1000Y0M0W0D" P="1" A="36500" />
</X>

I tried this but did not get any values:

select 
    cast(RentalValueAmount as XML).value('data(/X/E)[1]','varchar(10)') as test
from dbo.units

I need to extract or return 36500 as a number using a query but I have not been able to do so. Obviously I do not know XML, so I would really appreciate the help.

Try this:

select 
cast(RentalValueAmount as XML).value('(/X/E)[1]/@A','varchar(10)') as test
from dbo.units

If the column is already of XML data type, you don't need to cast it to XML again.

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