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 2005 Forums
 Transact-SQL (2005)
 Calculate %

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2008-04-15 : 15:21:53
I want to calculate rate as percentage using code below:

SELECT OrderBy, NumShiped/Total AS Rate FROM Order

I always got Rate = 0 even thought NumShiped = 80 and Total = 100

What is problem?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-15 : 15:27:02
You need to convert one of them to decimal. 80/100 equals 0 when it comes to the integer data types.

SELECT OrderBy, CONVERT(decimal(18,2), NumShiped)/Total AS Rate FROM Order

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2008-04-15 : 15:34:13
It is working. Thank you!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-04-16 : 03:27:10
Also refer http://sqlblogcasts.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
   

- Advertisement -