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)
 Simple Arithmetic/Variable Question

Author  Topic 

zavier
Yak Posting Veteran

50 Posts

Posted - 2003-12-15 : 11:34:09
Hello All

I have a simple question. I have the following in a stored procedure:

DECLARE @T1COUNT INT
DECLARE @T2COUNT INT
DECLARE @RATIO DECIMAL


SELECT @T1COUNT=COUNT(*) FROM #TABLE1

SELECT @T2COUNT=COUNT(*) FROM #TABLE2

SET @RATIO = @T1COUNT/@T2COUNT

PRINT @RATIO

This doesn't seem to work. The variables @T1COUNT and @T2COUNT have the correct numbers in them but when I do the division the @RATIO variable contains zero. I think my syntax is incorrect. If anyone can lend a hand I would appreciate it. Thanks

SamC
White Water Yakist

3467 Posts

Posted - 2003-12-15 : 11:43:04
Integer division?

5 / 10 = 0

Gotta convert one or both integers to real.

Here's a cheat:

set @ratio = 1.0 * @T1count / @T2count

Here's a better solution

set @ratio = CAST(@T1count AS DECIMAL) / CAST(@T2count AS DECIMAL)

I'm not sure what the default precision is on DECIMAL, so I always specify it like:

CAST(@T1count AS DECIMAL(5,2))

Go to Top of Page

zavier
Yak Posting Veteran

50 Posts

Posted - 2003-12-15 : 12:08:14
Thanks very much. I knew it was something I was overlooking.
Go to Top of Page
   

- Advertisement -