Home All Groups Group Topic Archive Search About
Author
10 Dec 2005 11:33 AM
marcmc
select 717306 * 3000
Server: Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.

What's the story with this?

Author
10 Dec 2005 11:53 AM
David Portas
marcmc wrote:
> select 717306 * 3000
> Server: Msg 8115, Level 16, State 2, Line 1
> Arithmetic overflow error converting expression to data type int.
>
> What's the story with this?

You didn't specify a datatype for the numbers so SQL Server has to
guess. The numbers don't have any decimals so the server guesses
INTEGER but that gives an overflow because the result is larger than
the maximum allowed for an integer (2147483647).

The solution is either to use decimal notation so that the server will
implicitly convert to a decimal:

SELECT 717306 * 3000.0 ;

or a much better solution is to specify the types explicity:

SELECT CAST(717306 AS NUMERIC(10,0)) * CAST(3000 AS NUMERIC(10,0)) ;

Implicit type conversion is a source of many errors so it pays to avoid
it where you can by always specfiying the types. Of course this isn't a
very typical example because both values are literals. More usually, at
least one would be an expression based on a column or variable, which
therefore is based on an explicit datatype of its own.

--
David Portas
SQL Server MVP
--
Author
10 Dec 2005 12:09 PM
marcmc
you the man,
thankyou david

Show quote
"David Portas" wrote:

> marcmc wrote:
> > select 717306 * 3000
> > Server: Msg 8115, Level 16, State 2, Line 1
> > Arithmetic overflow error converting expression to data type int.
> >
> > What's the story with this?
>
> You didn't specify a datatype for the numbers so SQL Server has to
> guess. The numbers don't have any decimals so the server guesses
> INTEGER but that gives an overflow because the result is larger than
> the maximum allowed for an integer (2147483647).
>
> The solution is either to use decimal notation so that the server will
> implicitly convert to a decimal:
>
> SELECT 717306 * 3000.0 ;
>
> or a much better solution is to specify the types explicity:
>
> SELECT CAST(717306 AS NUMERIC(10,0)) * CAST(3000 AS NUMERIC(10,0)) ;
>
> Implicit type conversion is a source of many errors so it pays to avoid
> it where you can by always specfiying the types. Of course this isn't a
> very typical example because both values are literals. More usually, at
> least one would be an expression based on a column or variable, which
> therefore is based on an explicit datatype of its own.
>
> --
> David Portas
> SQL Server MVP
> --
>
>

AddThis Social Bookmark Button