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)
 Why... Calculation problems

Author  Topic 

leahsmart
Posting Yak Master

133 Posts

Posted - 2005-01-12 : 05:09:24
Why does the below output 0 when it should output 33.3333333333333

DECLARE @Score Float
SET @Score = ((80 - 60) / 60) * 100
SELECT @Score

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-01-12 : 05:19:29
maths is being done in INTEGER format....see T-SQL rules re maths.
change (at least) one number along the way to have a decimal place.

ie ((80.0 - 60) / 60) * 100
Go to Top of Page

leahsmart
Posting Yak Master

133 Posts

Posted - 2005-01-12 : 05:24:12
Is there anyway of forcing it calculate it correctly without adding decimal places to any of the numbers?

These numbers will be variables that a user has entered. For example ((B - A) / A) * 100

Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-01-12 : 08:28:39
i think you must use the CONVERT clause...or you could do (((B - A) / A) * 100) * 1.0
or ensure the variables being input satisfy the requirements of the formula...ie force (append) a decimal place in the data being input.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-01-12 : 09:23:03
declare your variables as decimal or whatever precision you need, not int. Or, as Andrew points out,multiple them by 1.0 -- but you must do it earlier in the calculation, or it will be "too late" to retain the decimal. i.e., ((B-A) * 1.0 / A) *100)*1.0

- Jeff
Go to Top of Page
   

- Advertisement -