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
SadagopankAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jan FranekCommented:
You need to convert @var1 to some more precise numeric type (eg. numeric(10,2) ) to get rid of 0 and convert whole expresion to the same type to get rid of truncation error:

select @var3 = convert(numeric(10,2),convert(numeric(10,2),@var1)/@var2)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jan FranekCommented:
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
0
Joe WoodhousePrincipal ConsultantCommented:
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).
0
SadagopankAuthor Commented:
thanks jan and joe ... the solution worked just fine.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Sybase Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.