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 error encountered.

Author  Topic 

tantcu
Yak Posting Veteran

58 Posts

Posted - 2012-07-13 : 09:16:25
Hello,

I have some data and it contains value 0 on it. When I set up the formula for example, cogs/sales. Because there are some sales with 0 value so that the query I set up with formula does not work when you divide something with the 0. Do you know how to fix that problem?

singularity
Posting Yak Master

153 Posts

Posted - 2012-07-13 : 09:21:54
Instead of doing cogs/sales, you can do:

case when sales = 0 then 0 else cogs/sales end
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-13 : 09:57:38
or

coalesce(cogs/nullif(sales,0),0)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-07-16 : 05:05:20
Beware of implicit convertions
http://beyondrelational.com/modules/2/blogs/70/posts/10825/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -