Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 calculation

Author  Topic 

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2006-06-08 : 03:17:58
Hi,
Please see why @X1 returns 0 whereas it should return a value of 50.
Thanks

declare @A1 int
declare @A2 int
declare @A3 int
declare @B1 int
declare @B2 int
declare @B3 int
declare @X1 int

set @A2 = 5
set @A3 = 5

set @B2 = 3
set @B3 = 2

select (@B2+@B3)
select (@A2+@A3)

set @X1=((@B2+@B3)/(@A2+@A3))*100
select @X1

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-06-08 : 03:35:35
Use

set @X1=((@B2+@B3)*1.0/(@A2+@A3))*100

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-08 : 03:35:51
integer divide by integer = integer
so 5 / 10 = 0
change to
set @X1=((@B2+@B3)*100/(@A2+@A3))


KH

Go to Top of Page

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2006-06-08 : 03:41:13
How about this?

set @X1=(convert(decimal, @B2+@B3)/convert(decimal, @A2+@A3))*100
or
set @X1=(convert(float, @B2+@B3)/convert(float, @A2+@A3))*100

Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-06-08 : 03:43:26
use decimal. float is approximate value (unless you want this)


KH

Go to Top of Page

fmardani
Constraint Violating Yak Guru

433 Posts

Posted - 2006-06-08 : 04:11:20
Solved.
Thanks
Go to Top of Page
   

- Advertisement -