Solved

Supressing Divide by Zero error

Posted on 2002-04-08
5
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

736 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