49

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 int because the resulting number is to big for an INT. So I tried to CAST to a BIGINT using the following

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

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

Robin Day
  • 100,552
  • 23
  • 116
  • 167

1 Answers1

87

Try converting it before summing. eg.

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

or

SELECT SUM(CAST(columnname AS BIGINT)) FROM tablename
Robin Day
  • 100,552
  • 23
  • 116
  • 167
  • SELECT (SUM(CAST(columnname AS BIGINT)) + SUM(CAST(columnname AS BIGINT)) + SUM(CAST(columnname AS BIGINT)))/xxxxx) as total_usage FROM tablename . I have been trying your solution but it still doesnt work – Nani3 Apr 12 '19 at 18:45