SQL 错误:将数字转换为数据类型 varchar 的算术溢出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8592470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:43:00  来源:igfitidea点击:

Error : Arithmetic overflow error converting numeric to data type varchar

sqlstored-procedures

提问by Neo

Error : Arithmetic overflow error converting numeric to data type varchar.

错误:将数字转换为 varchar 数据类型时出现算术溢出错误。

Getting error at this line why and what should be changed?

在这一行出现错误为什么以及应该改变什么?

CONVERT(VARCHAR(8),CONVERT(DECIMAL(8,4),((CurrentLoans.Price - PreviousLoans.Price) / PreviousLoans.Price) * 100)) 

回答by JNK

Here's at least one issue:

这里至少有一个问题:

CONVERT(VARCHAR(8),CONVERT(DECIMAL(8,4))

CONVERT(VARCHAR(8),CONVERT(DECIMAL(8,4))

The Decimal(8,4)indicates 8 numeric digits, 4 to the right of the decimal. This does NOTaccount for the actual decimal character, so you potentially have a value like:

Decimal(8,4)表示8位数字,4到小数点右边。但这占实际小数字符,所以你可能有这样一个值:

1234.5678

1234.5678

which is a valid Decimal(8,4)but won't fit in a varchar(8).

这是一个有效Decimal(8,4)但不适合varchar(8).

回答by user1944720

I know this is an old post but I just wanted to say thanks. I was having this exact problem and what was most annoying was that it gave the error when selecting from a VIEW , but did not give the error when I used the select statement from the VIEW and pasted it and inserted it into a TEMP TABLE!!

我知道这是一个旧帖子,但我只是想说声谢谢。我遇到了这个确切的问题,最烦人的是它在从 VIEW 中选择时给出了错误,但是当我使用 VIEW 中的 select 语句并将其粘贴并插入到临时表中时没有给出错误!!

example:

例子:

select * from dvView --worked
select * from dvView where product = '5' --Broke!
--BUT
select * from #Temp_table_dvView --worked!
select * from #Temp_table_dvView where product = '5' --worked!

in the end, I had to changed a part in the view from

最后,我不得不将视图中的一部分从

select cast(productNumber as nvarchar(1), etc...

to

select cast(productNumber as nvarchar(2), etc...

and it worked.

它奏效了。

but weird that the error I got was

但奇怪的是我得到的错误是

Arithmetic overflow error converting numeric to data type varchar.

instead of the one that reads

而不是那个阅读

Data would be truncated

or whatever...

管他呢...

food for thought.

深思熟虑。