[英]Error converting varchar to numeric in Stored Procedure
I am trying to insert record into a SQL Server
table from a XML file.我正在尝试将记录从 XML 文件插入
SQL Server
表。 The XML file has some entries for Depth
column with values that are little different for ex. XML 文件有一些
Depth
列的条目,其值与 ex 的值略有不同。 -8.67991800817151E-0 -8.67991800817151E-0
Can someone please guide me how to convert that value into decimal and insert into table?有人可以指导我如何将该值转换为十进制并插入表格吗? Thanks.
谢谢。
Here is my stored procedure:这是我的存储过程:
CREATE PROCEDURE [dbo].[IProjectData]
@xml XML
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO DHDStrainSet
SELECT
DHDStrainSet.value('(StrainID/text())[1]','VARCHAR(255)') AS StrainID,
DHDStrainSet.value('(Depth/text())[1]','decimal(18,3)') AS Depth,
DHDStrainSet.value('(Strain/text())[1]','decimal(18,10)') AS Strain,
DHDStrainSet.value('(ProjectID/text())[1]','VARCHAR(50)') AS ProjectID
FROM
@xml.nodes('/RSM3DDB/DHDStrainSet') AS TEMPTABLE(DHDStrainSet)
END
Sample XML file is:示例 XML 文件是:
<?xml version="1.0" standalone="yes"?>
<RSM3DDB>
<DHDStrainSet>
<StrainID>SB 1_1ef50386</StrainID>
<Depth>-5.001987E-9</Depth>
<Strain>0.99</Strain>
<ProjectID>Hoop</ProjectID>
</DHDStrainSet>
</RSM3DDB>
The XML file has some entries for Depth column with values that are little different for ex.
XML 文件有一些 Depth 列的条目,其值与 ex 的值略有不同。 -8.67991800817151E-0.
-8.67991800817151E-0。 Can someone please guide me how to convert that value into decimal?
有人可以指导我如何将该值转换为十进制吗?
Looking at the value -8.67991800817151E-0
can't be converted to a decimal
, you would need to look at converting it to a float
.查看值
-8.67991800817151E-0
无法转换为decimal
,您需要查看将其转换为float
。
So this line:所以这一行:
DHDStrainSet.value('(Depth/text())[1]','decimal(18,3)') AS Depth,
Need's to be changed to (will fix the error):需要更改为(将修复错误):
DHDStrainSet.value('(Depth/text())[1]','float') AS Depth,
So in all you said you wanted a decimal(10,3)
, you could convert that value to a decimal(10,3)
for example:因此,在您所说的所有内容中,您想要一个
decimal(10,3)
,您可以将该值转换为decimal(10,3)
例如:
convert(decimal(10,3), DHDStrainSet.value('(Depth/text())[1]','float')) AS Depth,
This will output: -8.68
这将 output:
-8.68
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.