divided by zero error in SQL please provide solution

  • divided by zero error in SQL please provide solution

  • If you have

    SELECT x / y

    and y is zero, that is an error. There is no solution as such, only a choice about what you would like to return in cases where the denominator is zero.

    What would you like to see? NULL, 0, x, something else?

    Or you could CATCH the error and take some other action.


  • You could use NULLIF or SET ARITHABORT OFF; SET ANSI_WARNINGS OFF;

    SET ARITHABORT OFF;
    SET ANSI_WARNINGS OFF;

    DECLARE @x as int = 1,
    @y as int = 0

    select @x/@y

    SET ARITHABORT ON;
    SET ANSI_WARNINGS ON;

    select @x / NULLIF(@y, 0)

     

     

  • .

Viewing 4 posts - 1 through 4 (of 4 total)

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