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)
 Convert INT to DECIMAL

Author  Topic 

tking1
Starting Member

22 Posts

Posted - 2010-12-03 : 12:49:46
Ok, I have the following query:

select nvarchar20 Category, case when count(tp_id)/12 > 0 then convert(decimal(10,2),(count(tp_id)/12)) else 0 end AVG


For the first row (111 rows/12), the results show as 9.00, but the actual # should be 9.25...so the conversion is just adding ".00" to the int value of 9. How do I get it to show the actualy decimal value 9.25?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-03 : 13:05:23
Use /12.0 instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tking1
Starting Member

22 Posts

Posted - 2010-12-03 : 13:27:36
Wow, that sure was an easy fix (I feel like such a newb). Thanks Tara!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-03 : 13:31:28
You're welcome, glad to help. It's due to an implicit conversion. Read this for more info: http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -