Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help to prevent "Divide By Zero Error" in SQL 3

Status
Not open for further replies.

beetlebailey

Programmer
Jan 4, 2005
51
US
Hopefully this is a simple question. I am using the following error formula within Excel 2000 which prevents the #DIV/0! from showing up in the spread sheet:
=IF(ISERROR(SUM(C3:C12)),0,SUM(C3:C12))

Now, I am trying to query a SQL server database with the following query and receive a "Divide by zero error":
Select Hour1/Tact1 as PCT1 from vpd where line=505

Is there a similar way to prevent such errors from occurring in SQL server 2000 ?

Thanks, "beetle
 
Code:
Select case when isnull(Tact1,0) = 0 then 0 else Hour1/Tact1 end as PCT1 from vpd where line=305

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Add this before your query and include it when running:

SET ARITHABORT OFF

Which means that the query will not terminate once it hits a divide by zero or overflow error.

Tim
 
SET ARITHABORT OFF works always only if ANSI_WARNINGS are turned off:
Code:
set arithabort off

-- works, returns NULL
set ansi_warnings off
select 2/0

-- blam!
set ansi_warnings on
select 2/0
In complex batches it is recommended to reverse these settings back after OFF is no more necessary.

Another way is to replace 0 with NULL. Then division will also return NULL:
Code:
Select Hour1/NULLIF(Tact1, 0) as PCT1 from vpd where line=505

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top