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 by zero

Author  Topic 

maevr
Posting Yak Master

169 Posts

Posted - 2008-06-30 : 05:07:04
select 1/2
select 1/2.0
The above produces different result based on decimals.

The problem is that field2 contains zero and nulls so that divide by zero problem arises, but sqrt should not return zeros.

select 5 / sqrt(0.49 * "field2")
from SS_FB

How can I get past this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-30 : 05:13:39
select ISNULL(5 / NULLIF(sqrt(0.49 * "field2"),0),0)
from SS_FB
Go to Top of Page

ranganath
Posting Yak Master

209 Posts

Posted - 2008-06-30 : 05:13:50
Hi,

Replace 0, Null with 1 in the Column Field2
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-30 : 05:14:05
use case statement.

case when field2 <> 0 then . .. else NULL end


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-30 : 05:19:44
quote:
Originally posted by ranganath

Hi,

Replace 0, Null with 1 in the Column Field2



replacing with 1 will let it pass but the result will be misleading right ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-30 : 05:32:04
quote:
Originally posted by ranganath

Hi,

Replace 0, Null with 1 in the Column Field2


better to show the result as 0 rather than just showing numerator value.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-30 : 05:36:16
in my opinion, best is to leave it as NULL.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-30 : 05:40:34
quote:
Originally posted by khtan

in my opinion, best is to leave it as NULL.


KH
[spoiler]Time is always against us[/spoiler]




in that case you wont require the ISNULL also
Go to Top of Page

maevr
Posting Yak Master

169 Posts

Posted - 2008-06-30 : 06:48:38
Thanks for your help, I made a case handling the 0.
Go to Top of Page
   

- Advertisement -