Divide by zero error encountered problem

  • 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>

  • 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

  • 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

  • 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-2 int

    SELECT @a=1, @b-2=0

    SELECT ISNULL(@a/NULLIF(@b,0),0)

    Razvan

  • 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

  • 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

     

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply