SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Division
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanjay5219
Posting Yak Master

231 Posts

Posted - 07/19/2013 :  09:01:17  Show Profile  Reply with Quote
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

231 Posts

Posted - 07/19/2013 :  09:07:04  Show Profile  Reply with Quote
Thanks

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

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 07/19/2013 :  10:47:18  Show Profile  Reply with Quote
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

India
52323 Posts

Posted - 07/20/2013 :  08:31:23  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 07/20/2013 :  16:38:29  Show Profile  Reply with Quote
Another perspective: http://myshallowsqlblog.wordpress.com/size-does-matter/

Edited by - James K on 07/20/2013 21:19:28
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000