In this article, we will be discussing about the “Divide by zero error encountered” error message in SQL Server.
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.
Example:
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.
Example Query:
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)
Strengthen you SQL Server Development Skills – Enroll to our Online Course!
Via the course, you will sharpen your SQL Server database programming skills via a large set of tips on T-SQL and database development techniques. The course, among other, features over than 30 live demonstrations!
(Lifetime Access/ Live Demos / Downloadable Resources and more!)
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)!
Example:
declare @denominator int
set @denominator=0
select 1/ISNULL(NULLIF(@denominator,0),1)
Concluding Remarks
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)!
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.
Views:7,266
1 thought on “Divide by zero error encountered”
Thanks! Approach 3 solved the problem I was having.
Comments are closed.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Read More
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Thanks! Approach 3 solved the problem I was having.