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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Divide by Zero Error

Author  Topic 

mufasa
Yak Posting Veteran

62 Posts

Posted - 2003-10-03 : 09:05:21
I would like to know if anyone has a work around divide by zero.

I have a view with calculations, which sometimes has a divide by zero error.

I know I can use the
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF

To stop the error, but you cannot use these commands in a view.

I use my views as the backbone for my reports, I know I could work around this in Access or Crystal reports, but I would like the SQL server to do most of the work.

Any help would be appreciated

Thanks
Mufasa

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-10-03 : 09:16:28
select col1/nullif(col2, 0) from t
Go to Top of Page

mufasa
Yak Posting Veteran

62 Posts

Posted - 2003-10-03 : 09:27:06
quote:
Originally posted by Stoad

select col1/nullif(col2, 0) from t



Thank-You Very Much

Too bad Books online do not give that example

Mufasa
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-10-03 : 10:55:17
Um, wouldn't it be

select col1 / case when col2 is null or col2 = 0 then 1 else col2 end

Sam

Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2003-10-03 : 10:56:57
Or better - if no result is wanted for divide by zero, then return null

SELECT CASE WHEN col2 IS NULL OR col2 = 0 THEN NULL ELSE col1 / col2 END

Sam
Go to Top of Page
   

- Advertisement -