• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7348
  • Last Modified:

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
0
Sadagopank
Asked:
Sadagopank
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now