Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sql-server

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.

like image 422
Septiana Fajrin Avatar asked Mar 08 '23 18:03

Septiana Fajrin


1 Answers

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.

convert(bigint, 8) * 1024 * 1048576
like image 146
Sean Lange Avatar answered Mar 11 '23 00:03

Sean Lange