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
 SQL Server Development (2000)
 Working with Percentage's

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-09 : 11:14:05
Hello all

How can I get a percentage as a 2 decimal point number?

I have a function that calculates the percentage:

CREATE FUNCTION dbo.fn_CalculatePercentages
(
@totalCount int
, @count int
)
RETURNS DECIMAL(5,2)
AS
BEGIN
DECLARE @percentage SMALLINT
SET @percentage = (100/CONVERT(DECIMAL,@totalCount))* @count
RETURN @percentage
END
GO


However, the result is always rounded (not sure whether rounded up or down):

97.00
45.00
34.00

Whereas I want something like this:

97.67
45.23
34.89

Thanks in advance

Hearty head pats

Bex
Aged Yak Warrior

580 Posts

Posted - 2006-02-09 : 11:17:35
Oh dear, how embarassing. I keep doing this recently.......working away on something, cannot see the woods for the trees, make a post, and then the forest clears!

Its because the variable is declared as a SMALLINT. DOH!



Hearty head pats
Go to Top of Page
   

- Advertisement -