SQL SERVER 2016 is not throwing dived-by-zero error.

Pavan Ramavath 0 Reputation points
2023-04-24T19:46:54.61+00:00

Im using SQL SERVER 2016 and it is not throwing dived-by-zero error. When I researched, I came to know that Starting with SQL Server 2012 (11.x), when a divide-by-zero error occurs during query execution, the database engine automatically converts the result of the expression to null and this behavior is referred to as a "silent truncation". Is this true ? Can you share some more information on this?

SQL Server | Other
{count} votes

4 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2023-04-24T20:20:13.0233333+00:00

    Eh, no, by default you get an error for division by zero in SQL 2016, look below: User's image

    It is possible to change the rules, though:

    User's image

    But the setting ANSI_WARNINGS OFF is a pure legacy setting that you should not use. (ARITHABORT is OFF by default in most context on the other hand. But it is on by default in SSMS and Azure Data Studio. Both settings must be off to prevent arithmetic errors to raise exceptions)


  2. ZoeHui-MSFT 41,491 Reputation points
    2023-04-25T06:08:59.04+00:00

    Hi @Pavan Naik,

    Could you please share the result from your side with the screenshot?

    In addition, I searched about silent nullification online and do not find useful information.

    From my side, it will get an error for division by zero.

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.


  3. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2023-04-26T19:51:38.69+00:00

    I have a SP with rule/logic which does the calculations. when i run the SP sometimes it throws divide by zero some times it doesn't.

    Since we don't see the code, it will have to be a bit of speculation. But I will assume that there are conditions that filters out the three problematic rows, so that they are not part of the result being returned.

    In this case, ideally, there should not be any exception at all, since logically there is no error. However, depending on the query plan SQL Server may filter first and then compute the division, or it may divide before filtering. When composing the query plan, the optimizer does not consider that an operation can fail and take precautions accordingly. So therefore the query can fail even if the bad rows are logically filtered out.

    A common way to avoid this is to write divisions like val1/nullif(val2, 0), so that you get a NULL for the division.

    0 comments No comments

  4. ZoeHui-MSFT 41,491 Reputation points
    2023-04-27T06:05:48.0233333+00:00

    Hi @Pavan Naik,

    I don't think Chatgpt is an enough perfect tool, don't believe everything it tells you.

    If you’d like to handle division by zero gracefully, you can use the NULLIF function. NULLIF takes two arguments: the expression of interest, and the value you want to override. If the first argument is equal to the second, then NULLIF returns NULL; otherwise, it returns the first argument.

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.