简体   繁体   English

将nvarchar转换为int时出错

[英]Error in Converting nvarchar to int

I have one table #DateDay_temp in which there is a column ExtraAdultPrice of type NVARCHAR(20) . 我有一个表#DateDay_temp ,其中有一个NVARCHAR(20)类型的ExtraAdultPrice NVARCHAR(20) When I am trying to run this statement in a stored procedure 当我试图在存储过程中运行此语句时

DECLARE @ExtraAdultPAX DECIMAL(10,2)

SELECT 
    @ExtraAdultPAX = SUM(CAST((CASE ISNULL(ExtraAdultPrice, '') 
                                  WHEN '' THEN 0 
                                  ELSE ExtraAdultPrice END) AS DECIMAL(10,2))) 
FROM #DateDay_temp;

When I am passing the value 54.56 in the ExtraAdultPrice " column, I get the error 当我在ExtraAdultPrice “列中传递值54.56时,我收到错误

Conversion failed when converting the nvarchar value '54.56' to data type int. 将nvarchar值'54 .56'转换为数据类型int时转换失败。

Help would be appreciated. 帮助将不胜感激。

Most likely this error happens because of the fact you have a 0 in your CASE statement; 很可能是因为您的CASE语句中有0而发生此错误; therefore, the "type" of the CASE statement is decided to be an INT - which is completely wrong here.... 因此, CASE语句的“类型”被确定为INT - 这里完全错误....

Try this instead: 试试这个:

SELECT 
    @ExtraAdultPAX = SUM(CAST((CASE ISNULL(ExtraAdultPrice, '') 
                                 WHEN '' THEN 0.00   <<== use "0.00" instead of just "0" !!
                                 ELSE ExtraAdultPrice 
                               END) AS DECIMAL(10,2))) 
FROM 
    #DateDay_temp;

Now, everything should be DECIMAL(10,2) and there shouldn't be any attempt to convert your values to an INT 现在,一切都应该是DECIMAL(10,2)并且不应该尝试将您的值转换为INT

由于小数点54.56不是int。

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

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