Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sanjay5219
Posting Yak Master

240 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

240 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

3873 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
52326 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

3873 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  
 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.02 seconds. Powered By: Snitz Forums 2000