## Divide by zero error encountered problem

 Author Message mamidimahesh-127681 SSC-Enthusiastic Group: General Forum Members Points: 180 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) Dan Collier Valued Member Group: General Forum Members Points: 67 Visits: 16 This will give your desired reults without error.Declare @Res int ,@denominator intSet @Res = 0 Set @denominator = 0-0Select @Res = CASE WHEN @denominator = 0 THEN 0 ELSE 5000/(@denominator) ENDSelect @Res as Result mamidimahesh-127681 SSC-Enthusiastic Group: General Forum Members Points: 180 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.00Select @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 Razvan Socol UDP Broadcaster Group: General Forum Members Points: 1437 Visits: 129 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 OFFSET ANSI_WARNINGS OFFSELECT 1/0 -- you will get nullYou 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 intSELECT @a=1, @b=0SELECT ISNULL(@a/NULLIF(@b,0),0)Razvan mamidimahesh-127681 SSC-Enthusiastic Group: General Forum Members Points: 180 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 Bruce Gilpin SSC Veteran Group: General Forum Members Points: 230 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 - queryvar2else begin select @res = 0end