Link to home
Start Free TrialLog in
Avatar of Sadagopank
Sadagopank

asked on

truncation error in sybase ?

hello experts,

I get truncation error when i try to do the following

set nocount on
declare @var1 int,
        @var2 int,
        @var3 numeric(10,2)
       
       
select @var1 = 5
select @var2 = 6

select @var3 = @var1/@var2

select @var3

go

I get a 0 in @var3 and a trucation error occured ..

basically i want to do a @var1/@var2 * 100 (to find out the % )

how can this be done ?

thanks
ASKER CERTIFIED SOLUTION
Avatar of Jan Franek
Jan Franek
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Other way how to get rid of truncation error:

set arithabort numeric_truncation off
select @var3 = convert(numeric(10,2),@var1)/@var2
set arithabort numeric_truncation on
Avatar of Joe Woodhouse
Joe Woodhouse

Even more simply:

select @var3 = 1.0*@var1/@var2

There is a hierarchy of datatypes. When the expression is just "@var1/@var2", Sybase sees only integers, and assumes you want integer division. By adding the 1.0 at the start of the expression, Sybase understands you're talking about numerics (as numerics take precedence over integers, so the entire expression is "promoted" to a numeric).
Avatar of Sadagopank

ASKER

thanks jan and joe ... the solution worked just fine.