Solved

Supressing Divide by Zero error

Posted on 2002-04-08
5
518 Views
Last Modified: 2010-05-18
I am connection to MS-SQL 7 using ASP.NET.

I use sql statements to do some calculations. Is there a way to suppress the divide by zero error. Instead of error can it return null or zero.?

Thank you...
0
Comment
Question by:fkmfkm
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
JamesT earned 100 total points
ID: 6925489
One way is to check for 0 prior to the calculation. For example:

SELECT SUM(CASE WHEN COLUMNA = 0 THEN 0 ELSE COLUMNB/COLUMNA END)
FROM TABLE_CALCULATION

You could also use a variation on IsNull assuming the column is null and not 0.
0
 
LVL 1

Expert Comment

by:dmeili
ID: 6926014
If you have a number of sql statements doing this it may be worthwhile to create a user defined function such as:

CREATE function dbo.divide_by_zero( @numerator decimal(38,19), @denominator decimal(38,19))
returns decimal(38,19)
as
begin
  declare @result decimal (21,2)
  if @denominator <= 0
    begin
      set  @result = 0.00
    end
  else
    begin
      set  @result = (@numerator/@denominator)
    end
  return(@result)
end

0
 

Author Comment

by:fkmfkm
ID: 6928310
I thought i can set something like ansi warning off to just ignore this error ?
0
 
LVL 2

Expert Comment

by:JamesT
ID: 6928345
Well, you have to use SET ARITHABORT OFF. If you have a divide by zero error it will return NULL.
0
 

Author Comment

by:fkmfkm
ID: 7035118
I can't create a udf cause i am using 7.0
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question