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
 General SQL Server Forums
 New to SQL Server Programming
 / (Divide) quandry

Author  Topic 

dmilam
Posting Yak Master

185 Posts

Posted - 2010-09-14 : 17:32:47
How can I get more than 0 with the first select, such as .5 as I would like to see?


select 1/2
----------
0



select 1/1.1
--------
0.909090



select 1/0
-----------
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-14 : 17:34:10
select 1/2.0

select 1/2*1.0

declare @i int, @j decimal(4,2)
select @i=1, @j=2
select @i/@j

The key is that it needs to be decimal.

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

Subscribe to my blog
Go to Top of Page

dmilam
Posting Yak Master

185 Posts

Posted - 2010-09-14 : 17:36:54
Aha, that makes sense. Thanks!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-14 : 17:38:40
You're welcome, glad to help.

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

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-14 : 17:41:45
Oh and this is known as data type precendence: http://msdn.microsoft.com/en-us/library/aa276874(SQL.80).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

dmilam
Posting Yak Master

185 Posts

Posted - 2010-09-14 : 18:01:17
Thanks again, Tara. Reminds me of operator precedence which helped me a good deal with the order of AND, OR, etc.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-09-15 : 10:54:15
Also refer this
http://beyondrelational.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 -