SQL 对 INT 求和时出现算术溢出错误,如何将其转换为 BIGINT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1377718/
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 03:27:35  来源:igfitidea点击:

Arithmetic overflow error when summing an INT, how do I cast it as a BIGINT?

sqlsql-servercastingmathbigint

提问by Robin Day

When I try to get the sum of a column from a table I get the error Arithmetic overflow error converting expression to data type intbecause the resulting number is to big for an INT. So I tried to CAST to a BIGINT using the following

当我尝试从表中获取列的总和时,我收到错误消息,Arithmetic overflow error converting expression to data type int因为结果数字对于 INT 来说太大了。所以我尝试使用以下命令转换为 BIGINT

SELECT CAST(SUM(columnname) AS BIGINT) FROM tablename

This gives me the same error. Any ideas what i'm doing wrong?

这给了我同样的错误。任何想法我做错了什么?

回答by Robin Day

Try converting it before summing. eg.

在求和之前尝试转换它。例如。

SELECT SUM(CONVERT(bigint, columnname)) FROM tablename

or

或者

SELECT SUM(CAST(columnname AS BIGINT)) FROM tablename