我有一个包含xml数据的表。由于某些原因,xml值中的某些字段具有不一致的数据。我需要相应地修改它。在下面的模板中我们可以看到字段txtCapacity,该值超出范围值。我需要将其转换为正确的格式使其成为浮点数?
Below is the table structure
Table XML_Capacity
( RID int,
xml_detail xml
)
Xml template
<Form>
<LT004>
<Field>
<id>txtDate</id>
<value>3/14/2017</value>
<tag />
<visible>true</visible>
<description>Install Date:</description>
<comment />
</Field>
<Field>
<id>txtAge</id>
<value />
<tag />
<visible>true</visible>
<description>Age:</description>
<comment />
</Field>
<Field>
<id>txtCapacity</id>
**<value>4.000000000000000e+003</value>**
<tag />
<visible>true</visible>
<description> Capacity:</description>
<comment />
</Field>
<Field>
<id>Status</id>
<value>Upgrade Repair Not Marked</value>
<tag />
<visible>true</visible>
<description>Status</description>
<comment />
</Field>
<Field>
<id>RemovedDate</id>
<value />
<tag />
<visible>false</visible>
<description>Date:</description>
<comment />
</Field>
I used the below code to modify but it is not allowing me to use convert
function inside that update statement
UPDATE XML_Capacity
SET xml_detail.modify('replace value of (/Form/L004/Field/text())[16] with (/FormValue/L004/Field/text())[16]')
Where
xml_detail.value('(/Form/L004/Field/node())[15]', 'varchar(45)') = 'txtCapacity'
答案 0 :(得分:0)
我不明白你的陈述
在下面的模板中,我们可以看到字段txtCapacity,该值超出范围值
我们怎么能看到这个?什么是超出范围?
这是常规方法,浮点值存储在XML中。试试这个
SELECT CAST(10.0/3 AS FLOAT(5)) AS SimpleFloat
,CAST(10.0/3 AS FLOAT(53)) AS MaxFloat
,CAST(10000.0/3 AS FLOAT(5)) AS SimpleFloatBig
,CAST(10000.0/3 AS FLOAT(53)) AS MaxFloatBig
FOR XML RAW,ELEMENTS
结果
<row>
<SimpleFloat>3.3333330e+000</SimpleFloat>
<MaxFloat>3.333333000000000e+000</MaxFloat>
<SimpleFloatBig>3.3333333e+003</SimpleFloatBig>
<MaxFloatBig>3.333333333000000e+003</MaxFloatBig>
</row>
这只是十分强大的科学记数法。您可以使用隐式转换轻松阅读此内容:
DECLARE @xml XML=
'<row>
<SimpleFloat>3.3333330e+000</SimpleFloat>
<MaxFloat>3.333333000000000e+000</MaxFloat>
<SimpleFloatBig>3.3333333e+003</SimpleFloatBig>
<MaxFloatBig>3.333333333000000e+003</MaxFloatBig>
</row>';
SELECT @xml.value(N'(/row/SimpleFloatBig/text())[1]',N'float(53)')
...返回3333.3333
。
在您的示例中,您正在执行某些操作非常危险:您尝试使用其序号(例如'(/Form/L004/Field/node())[15]'
)来解决该节点。您应该使用正确的XQuery predicate
来获取正确的节点。检查这个完全有效的测试场景:
CREATE DATABASE TestDB;
GO
USE TestDB;
CREATE TABLE XML_Capacity
( RID int,
xml_detail xml
);
INSERT INTO XML_Capacity(RID,xml_detail)
VALUES(1,
N'<Form>
<LT004>
<Field>
<id>txtDate</id>
<value>3/14/2017</value>
<tag />
<visible>true</visible>
<description>Install Date:</description>
<comment />
</Field>
<Field>
<id>txtAge</id>
<value />
<tag />
<visible>true</visible>
<description>Age:</description>
<comment />
</Field>
<Field>
<id>txtCapacity</id>
<value>4.000000000000000e+003</value>
<tag />
<visible>true</visible>
<description> Capacity:</description>
<comment />
</Field>
<Field>
<id>Status</id>
<value>Upgrade Repair Not Marked</value>
<tag />
<visible>true</visible>
<description>Status</description>
<comment />
</Field>
<Field>
<id>RemovedDate</id>
<value />
<tag />
<visible>false</visible>
<description>Date:</description>
<comment />
</Field>
</LT004>
</Form>');
SELECT xml_detail.value(N'(/Form/LT004/Field[(id/text())[1]="txtCapacity"]/value/text())[1]',N'float')
FROM XML_Capacity;
GO
DECLARE @NewValue FLOAT=12.345;
UPDATE XML_Capacity
SET xml_detail.modify(N'replace value of (/Form/LT004/Field[(id/text())[1]="txtCapacity"]/value/text())[1] with sql:variable("@NewValue")')
WHERE RID=1;
SELECT xml_detail.value(N'(/Form/LT004/Field[(id/text())[1]="txtCapacity"]/value/text())[1]',N'float')
FROM XML_Capacity;
USE master;
GO
DROP DATABASE TestDB;
GO
返回值为4000
, - 修改后为12.345
。