Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Divide by zero error encountered problem Expand / Collapse
Author
Message
Posted Friday, May 07, 2004 9:54 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 11, 2009 2:45 AM
Points: 40, Visits: 15
Hi,

I am facing problem with following query.

Declare @Res int
Set @Res = 0
Select @Res = 5000/(0-0)
Select @Res as Result

If I execute the above query it gives me "Divide by zero error encountered" error . Instead of geting this error can i get Result as 0(whenever this error comes). Is there anyway to do this?

Regards,
Bobby(UserID=132570) <script language=JavaScript></script>
Post #115040
Posted Sunday, May 09, 2004 9:00 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, June 20, 2013 4:47 PM
Points: 17, Visits: 16

This will give your desired reults without error.

Declare @Res int ,@denominator int
Set @Res = 0
Set @denominator = 0-0
Select @Res = CASE WHEN @denominator = 0 THEN 0 ELSE 5000/(@denominator) END
Select @Res as Result

Post #115070
Posted Sunday, May 09, 2004 11:01 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 11, 2009 2:45 AM
Points: 40, Visits: 15

Hi,

Thanks .  This solves my problem.  But there is another poblem with this.  In my application, all sql statements will be build by my front end.  In these statements "/" operator might be there or not.  Sometimes "/" operator may come more than once.  Please have a look of some examples which my application is generated.

IF .00 > 0 
begin 
  Select Result =  12000.00
end 
ELSE 
begin 
  Select Result =  0
end

IF 12000.00<6500 
begin 
  Select Result =  12000.00*0.0175
end 
ELSE
begin 
  Select Result =  0
end

Declare @Res int
Set @Res = 0
Select @Res = 12000.00
Select @Res as Result

Declare @Res int
Set @Res = 0
Select @Res = .00/(.00-.00)
Select @Res as Result

Now u suggest me how can I proceed.  If i get a error "Divide by zero error encountered" how can i make that error as "0".  My schedule is geting delayed because of this problem

Please help me in this.........

Bobby

Post #115074
Posted Monday, May 10, 2004 12:04 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 06, 2013 2:16 AM
Points: 693, Visits: 124

If you want cannot change the code, but you want to ignore divide by zero errors (to get null instead), you can use the ARITHABORT and ANSI_WARNINGS settings, like this:

SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT 1/0 -- you will get null

You will also get a warning (but not an error). If you want to suppress the warning too, use SET ARITHIGNORE ON.

Warning: using these options may cause some other problems, including the fact that you can't use indexes on computed columns.

My favourite way to handle such errors (without changing the default settings) is:

DECLARE @a money, @b int
SELECT @a=1, @b=0
SELECT ISNULL(@a/NULLIF(@b,0),0)

Razvan




Post #115077
Posted Monday, May 10, 2004 12:38 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, February 11, 2009 2:45 AM
Points: 40, Visits: 15

Hi Razvan,

Thanks a lot.  As of now i am not using index in my database.  Anyway thanks for giving me the warning.  I will keep in mind.

Bobby

Post #115081
Posted Monday, May 10, 2004 9:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, June 01, 2009 7:06 PM
Points: 172, Visits: 11

I'm not sure what "0-0" means.  If your demonator is a variable or a computation involving variables than you can then you can do somthing like this:

delcare @res int (I assume you know that this will truncate the decimal portion of your result)

if @queryvar1 - @queryvar2 <> 0 begin

  select @res = 5000 / @queryvar1 - queryvar2

else begin

  select @res = 0

end

 

Post #115189
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse