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)
 Division

Author  Topic 

sanjay5219
Posting Yak Master

240 Posts

Posted - 2013-07-19 : 09:01:17
Hi All,

Result of this query is returning 0 actually I need 0.04 as an answer.

Select cast(4/95 as float)

Please suggest

sanjay5219
Posting Yak Master

240 Posts

Posted - 2013-07-19 : 09:07:04
Thanks

Select Cast(1 as Decimal)/Cast(3 as Decimal)
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-19 : 10:47:18
As you found out, this is because of integer division. If any one of the participants in the arithmentic expression are of floating point type, then that will force non-integer division. So for example, people do this:
SELECT 4.0/95
Also, when you cast to DECIMAL, give a precision and scale so you know what you are getting rather than letting SQL Server default.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-20 : 08:31:23
quote:
Originally posted by sanjay5219

Thanks

Select Cast(1 as Decimal)/Cast(3 as Decimal)




see

http://visakhm.blogspot.in/2010/02/importance-of-specifying-length-in.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-07-20 : 16:38:29
Another perspective: http://myshallowsqlblog.wordpress.com/size-does-matter/
Go to Top of Page
   

- Advertisement -