Solved

Supressing Divide by Zero error

Posted on 2002-04-08
5
513 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now