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)
 One more question....quick this time.

Author  Topic 

label
Posting Yak Master

197 Posts

Posted - 2003-04-10 : 16:03:49
I'm trying to calculate a percentage.

The (total number of active users/total registered in country)

Example:

quote:


Declare @active_users int,
@country_count int,
@percent_active float

Select @percent_active=(@active_users/@country_count)
print @percent_active




This was working fine for a while. However, the last several times I've run this query I've been getting all "0's" as my result.

In fact, if I plug in the number "71" as @active_users and "156" as the @country_count and divide them using the above statement, I get a "0" as my answer.

This is one of my biggest gripes about SQL - it's difficult to work with numbers, int, floats and decimals when doing calculations.

Does anyone have any idea why I'm getting a "0" when I divide 71/156??


X002548
Not Just a Number

15586 Posts

Posted - 2003-04-10 : 16:15:59
Intergers don't have decimal places. and since your division takes you to a decimal number only, even if you multiplied it by 100, you'd still get 0.

Either change your declartion to be deciaml, or do something like:

Select Convert(Decimal(9,2),75)/Convert(Decimal(9,2),156)


Good Luck

btw did you eliminate all thos sql statements? Did any of the suggestions do what you needed?

Brett

8-)
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2003-04-10 : 16:20:13
quote:

Intergers don't have decimal places. and since your division takes you to a decimal number only, even if you multiplied it by 100, you'd still get 0.

Either change your declartion to be deciaml, or do something like:

Select Convert(Decimal(9,2),75)/Convert(Decimal(9,2),156)




Thanks. I'm trying this now.

quote:
[b]
btw did you eliminate all thos sql statements? Did any of the suggestions do what you needed?



I'm sure I'll learn a ton from examining all of the suggestions in the other thread....at this point though I'm just trying to get some valid results back.

After that, I'll focus on performance issues.

Thanks so much for your help!



Edited by - label on 04/10/2003 16:20:53
Go to Top of Page

label
Posting Yak Master

197 Posts

Posted - 2003-04-10 : 16:20:14
I appear to have double posted.

Edited by - label on 04/10/2003 16:21:10
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-04-10 : 16:32:10
That's one way to get your count up

Brett

8-)
Go to Top of Page
   

- Advertisement -