如何解决msg8115、16级、状态2、2行算术溢出错误将表达式转换为数据类型int ?

时间:2020-12-08 16:36:28

I have a formula like this. But the formula has an error. Please help me.

我有一个这样的公式。但是这个公式有一个误差。请帮助我。

select 
    [DAY] as [DAY],
    [Name] as [Name],
    ((cast([columnA] + [columnB] + [columnC] as bigint) * 1000) / NULLIF(8 * 1024 * 1048576, 0)) as [TotalColumn]
from 
    [TableA]

Error message:

错误信息:

Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type int.

Msg 8115,第16级,状态2,第2行算术溢出错误转换表达式到数据类型int。

2 个解决方案

#1


0  

I am not sure what you are trying at your end, but you can use NUMERIC datatype as the results can contain decimal places.

我不确定您正在尝试什么,但是您可以使用数字数据类型,因为结果可以包含小数部分。

select 
'Monday' as [DAY],
'Septiana Fajrin' as [Name],
((cast('5' + '5' + '5' as Numeric) * 1000) / (convert(Numeric, 8) * 1024 * 1048576))as [TotalColumn]  

Result

DAY Name    TotalColumn
Monday  Septiana Fajrin 0.000064610503613

#2


3  

Since your dividend is a bigint I suspect you will need the divisor to also be a bigint. Since you have some integer literals the math will attempt to put that in an int and it is too large. You can however force the divisor to be a bigint.

由于你的红利是一个大整数,我猜你需要除数也是一个大整数。因为你有一些整数,数学会尝试把它放在一个整数里,它太大了。但是,你可以强制除数是一个bigint。

convert(bigint, 8) * 1024 * 1048576

#1


0  

I am not sure what you are trying at your end, but you can use NUMERIC datatype as the results can contain decimal places.

我不确定您正在尝试什么,但是您可以使用数字数据类型,因为结果可以包含小数部分。

select 
'Monday' as [DAY],
'Septiana Fajrin' as [Name],
((cast('5' + '5' + '5' as Numeric) * 1000) / (convert(Numeric, 8) * 1024 * 1048576))as [TotalColumn]  

Result

DAY Name    TotalColumn
Monday  Septiana Fajrin 0.000064610503613

#2


3  

Since your dividend is a bigint I suspect you will need the divisor to also be a bigint. Since you have some integer literals the math will attempt to put that in an int and it is too large. You can however force the divisor to be a bigint.

由于你的红利是一个大整数,我猜你需要除数也是一个大整数。因为你有一些整数,数学会尝试把它放在一个整数里,它太大了。但是,你可以强制除数是一个bigint。

convert(bigint, 8) * 1024 * 1048576