Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Decimals calculated as Int. 1

Status
Not open for further replies.

FailSafe

Programmer
Mar 16, 1999
14
ZA
I have this strange problem where a Calculation in my Stored Procedure only shows the Int Value. The Code below always returns 0 or 1 and never the decimal value. Can some on help?

DECLARE @HAMS int,
@SPAMS int,
@GOOD int,
@BAD int,
@SCORE decimal(14,3)
SET @HAMS = (SELECT dbo.Params.BayeHam FROM dbo.Params WHERE Config = 0)
SET @SPAMS = (SELECT dbo.Params.BayeSpam FROM dbo.Params WHERE Config = 0)
SET @GOOD = (SELECT dbo.Tokens.Hams FROM dbo.Tokens WHERE Token =@TOKEN)
SET @BAD = (SELECT dbo.Tokens.Spams FROM dbo.Tokens WHERE Token =@TOKEN)
IF @HAMS < 0 SET @HAMS = 0
IF @SPAMS < 0 SET @SPAMS = 0
IF @SPAMS = 0 OR @HAMS = 0
BEGIN
IF @HAMS > 0 SET @SCORE = 0
IF @SPAMS > 0 SET @SCORE = 1
END
ELSE
BEGIN
SET @SCORE =CONVERT(DECIMAL(14,2), (@BAD / @SPAMS) / ((@GOOD / @HAMS) + (@BAD / @SPAMS)))
IF @SCORE > 1 SET @SCORE = 1
IF @SCORE < 0 SET @SCORE = 0
END
--SET @SCORE = 0.99
UPDATE dbo.Tokens
SET dbo.Tokens.Score = (@SCORE)
WHERE dbo.Tokens.Token = @TOKEN


 
Because the variables @BAD and @GOOD are integers each divide will result as an integer.

One solution is to declare these as decimal.

Also note you declare @Score as decimal(10,3) but convert to deciaml(10,2)
 
Duh - Looks like I had a blonde moment :)
Thanks for the reply, sometimes the more you look at something the more invisible it becomes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top