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.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

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

     

     

  • .

    • This reply was modified 3 months, 4 weeks ago by  Jonathan AC Roberts. Reason: duplicate deleted
  • This was removed by the editor as SPAM

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

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