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 in Computed Column

Author  Topic 

Lesnet
Starting Member

2 Posts

Posted - 2008-10-15 : 18:41:30
Newbie here Team - sorry...

I'm building my first application from scratch. Its a light duty application for tracking OSHA accident rates. How do I avoid the divide by zero error in the following computed column statement when the NumberRecIndcidents is zero?

(CONVERT([decimal]6,2),([HrsWorked]/[NumberRecIncidents]))

Thanks in advance for helping.
Scott

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-15 : 18:50:01
You can use a CASE statement.

CASE WHEN NumberRecIncidents THEN 0 ELSE YourFormula END

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

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-10-15 : 20:55:13
Or use Nullif in denominator
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-15 : 22:26:26
What would you set the denominator to then? You have to avoid the error altogether.

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

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-10-15 : 23:11:00
I am talking about this one: or SET ARITHABORT OFF and SET ANSI_WARNINGS OFF .

quote:
Originally posted by Lesnet

Newbie here Team - sorry...

I'm building my first application from scratch. Its a light duty application for tracking OSHA accident rates. How do I avoid the divide by zero error in the following computed column statement when the NumberRecIndcidents is zero?

(CONVERT([decimal]6,2),ISNULL([HrsWorked]/NULLIF[NumberRecIncidents],0),0))
Thanks in advance for helping.
Scott



Go to Top of Page
   

- Advertisement -