Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic overflow w/ bigints in SQL Server 2008

I'm having a problem with arithmetic overflow when converting to bigint in SQL Server 2008. The following is confusing me.

This works:

select 58356453 * 228204732751

Answer: 13317218761161292203

But, all other attempts that involve explicit conversion fail.

select convert(bigint, 58356453 * 228204732751)

result: arithmetic overflow

begin 
    declare @key bigint = 58356453,
        @workingVal bigint,
        @primeMultiplier1 bigint = 228204732751;
    set @workingVal = @key * @primeMultiplier1;
end;

result: arithmetic overflow

What am I missing here? I've also tried "cast(BigNumber as bigint)" (don't think it's any different from convert), and various combinations of conversion before/after arithmetic operations.

And, this is on Windows Server 2008 x64

like image 355
user542516 Avatar asked Dec 14 '10 20:12

user542516


1 Answers

The maxiumum value for a bigint is

9223372036854775807

you're returning

13317218761161292203.

I suggest casting to a decimal that has enough precision.

like image 97
Mike M. Avatar answered Nov 13 '22 09:11

Mike M.