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
 General SQL Server Forums
 New to SQL Server Programming
 Working out percentage

Author  Topic 

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-11-26 : 08:57:58
I'm sure this is really simple, but if I use the following why do I get 0?

select (50 / 200) * 100

I'd expect to see 25

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-11-26 : 09:02:50
Managed to figure this out althought it seems a bit strange to have to do this:

select convert(decimal(18,2),(1.0 * 50/200 * 100))
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-26 : 09:03:27
integer arithmetic - both ints so result is truncated to int
try
select (50 / 200) * 100.0
or
select 1.0 * (50 / 200) * 100



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-11-26 : 10:36:46
More informations at
http://beyondrelational.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

chris_cs
Posting Yak Master

223 Posts

Posted - 2010-11-26 : 11:24:10
Thanks for the info guys!
Go to Top of Page
   

- Advertisement -