简体   繁体   English

从SQL Server 2012中的XML列中提取值

[英]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 : 这是一个包含在名为Units的表中的名为RentalValueAmount的XML列中的XML数据的示例:

<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. 我需要使用查询来提取或返回36500作为数字,但我无法这样做。 Obviously I do not know XML, so I would really appreciate the help. 显然我不了解XML,因此我非常感谢您的帮助。

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. 如果该列已经是XML数据类型,则无需再次将其转换为XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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