Arithmetic overflow error converting int to data type numeric.

Christopher Jack 1,616 Reputation points
2022-04-12T12:55:43.407+00:00

Hi,

I am trying to return a percentage number - the max values I am working with is 10,000

My code is

case when tt.[Under £20] <> 0 then
     ( CAST(tt.[Under £20] as numeric(5,5)) /CAST(tt.Total as numeric(5,5))*100) else 0
     end as '%',

Can anyone help?

Thanks

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Olaf Helper 47,586 Reputation points
    2022-04-12T13:05:19.033+00:00

    converting int to data type numeric.

    Your SQL code is more then a bit wrong.
    numeric(5, 5) means a total size of 5 digits and 5 decimal place = not leading digit before decimal; that can't work.
    Use numeric(5,0) or better numeric(10, 5).

    See https://learn.microsoft.com/en-us/sql/t-sql/data-types/decimal-and-numeric-transact-sql?view=sql-server-ver15


1 additional answer

Sort by: Most helpful
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-04-12T13:07:26.543+00:00

    Don't use numeric 5,5, use something like numeric(12,2) Or alternatively let SQL Server do conversion for you and just round the result, e.g.

    cast(case when tt.[Under £20] <> 0 then
    (tt.[Under £20]*1.0 /tt.Total)*100) else 0
    end) as numeric(10,2)) as '%',

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.