Solved

Supressing Divide by Zero error

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

867 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

19 Experts available now in Live!

Get 1:1 Help Now