About the “Divide by zero error encountered” error message
Well, as the error message says, whenever we have a division within a SQL query and the denominator has the value of zero (0) we get this error.
The exact error we get in SQL Server is:
Msg 8134, Level 16, State 1, Line [Here goes the line of the denominator in your query]
Divide by zero error encountered.
So, which is the best way of addressing this issue? As it is data-related we should be precautious anyway when having a division within a query and effectively control the denominator values for handling the case where a zero might be produced.
Just for reproducing the divide by zero error, consider the following example query:
declare @denominator int set @denominator=0 select 1/@denominator
Different Approaches for Handling the Issue
There are a few approaches of handling such problem. Here I present three.
Approach 1 – Using the “SET ANSI_WARNINGS OFF” Command
By using the “SET ANSI_WARNINGS OFF” command just right before the rest of the queries, it will allow your query that produces the error not to stop the execution of the rest of the queries.
SET ANSI_WARNINGS OFF declare @denominator int set @denominator=0 select 1/@denominator .... Other queries go here
Approach 2 – Using the CASE Statement
By using the CASE statement it is possible to check the denominator value for a zero and if it is so you can use 1 in order for the division not to fail.
declare @denominator int set @denominator=0 select 1/(case @denominator when 0 then 1 else @denominator end)
Alternatively, you can create a custom scalar-valued function that given an input parameter, it can check for a zero and if it is encountered it can return 1 else it should return the input:
CREATE FUNCTION check_denominator ( -- Function parameter @input int ) RETURNS int AS BEGIN -- Declare local variable DECLARE @result int -- Check for 0, if so then return 1 else return the input SET @result =(SELECT (CASE @input when 0 then 1 else @input end)) -- Return the result RETURN @result END GO
Then you can use the above function as follows:
declare @denominator int set @denominator=0 select 1/dbo.check_denominator(@denominator)
Approach 3 – Using the NULLIF Function
Yep, by using the NULLIF function it is possible to handle the issue of a zero denominator.
But how? 🙂
The NULLIF function takes two arguments and if they have equal values it then returns a NULL.
The idea here is to compare the denominator value with a zero via NULLIF and if it returns a NULL then to handle it with the ISNULL function (by placing the number 1)!
declare @denominator int set @denominator=0 select 1/ISNULL(NULLIF(@denominator,0),1)
Which of the above three approaches is the best one? Well, this is up to you 🙂
Personally I do not prefer Approach 1 as it does not solve the problem but rather “says” to SQL Server to ignore it.
So we have Approach 2 and 3 left. Approach 2 looks appealing but still I would only use it with a function.
My personal opinion is that Approach 3 is the best one; it just uses two built-in SQL Server functions and you do not need to write much additional code for handling a zero denominator!
Tip: Also, whenever you have a division in your query keep in mind that if you use only integer variables (like in this example 🙂 and the calculated denominator value is below zero it will return a zero so be careful with that as well (you can use float or decimal instead)!
Check our Master Class “Essential SQL Server Administration Tips”
If you really want to learn sophisticated SQL Server administration techniques, then you should check our on-demand online course titled “Essential SQL Server Administration Tips” (special limited-time discount included in link).
Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!
Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals (SQL Database for Beginners)
- The Philosophy and Fundamentals of Computer Programming
- Introduction to Data Science and SQL Server Machine Learning
- Introduction to Azure SQL Database
- SQL Server 2019: What’s New
- Entity Framework: Getting Started (Ultimate Beginners Guide)
- How to Import and Export Data in SQL Server
- Get Started with SQL Server in 30 Minutes
- A Guide on How to Start and Monetize a Successful Blog
Check Some of Our Other SQL Server Articles:
- Essential SQL Sever Administration Tips
- How to Patch a Standalone SQL Server Instance
- The SQL Server Browser Service and UDP Port 1434
- The Maximum Number of Concurrent Connections Setting in SQL Server
- Top 10 SQL Server DBA Daily Tasks List
- There is no SQL Server Failover Cluster Available to Join
- Encrypting a SQL Server Database Backup
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
Artemakis Artemiou is a Senior SQL Server Architect, Author, and a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 15 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and TechHowTos.com. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA).