简体   繁体   English

在存储过程中将 varchar 转换为数字时出错

[英]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.

相关问题 在存储过程中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in stored procedure 存储过程-将数据类型varchar转换为数值时出错 - Stored procedure - Error converting data type varchar to numeric 存储过程错误算术溢出错误将数字转换为数据类型 varchar - Stored Procedure Error Arithmetic overflow error converting numeric to data type varchar 将varchar转换为数值数据类型的算术溢出错误。 找不到存储过程“。”? - Arithmetic overflow error converting varchar to data type numeric. Could not find stored procedure ''.? 在存储过程(参数化)计算中将varchar转换为数值类型的算术溢出错误 - Arithmetic overflow error converting varchar to data type numeric in the stored procedure (parameterised) caluculation 在我的 SQL Server 存储过程中将数据类型 varchar 转换为数字时出错 - Error converting data type varchar to numeric in my SQL Server stored procedure 将varchar转换为数字时出错 - Error converting varchar to numeric 将数据类型varchar转换为存储过程中的int错误时出错 - Error converting data type varchar to int error in stored procedure 将数据类型varchar转换为存储过程中的bigint时出错 - Error converting data type varchar to bigint in stored procedure 从代码运行存储过程时将varchar转换为datetime时出错 - Error converting varchar to datetime when running stored procedure from code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM