How to Avoid the “divide by Zero" Error in SQL?
Last Updated :
14 Aug, 2024
In SQL, performing division operations can sometimes lead to errors, particularly when the divisor is zero. In this article, We will learn about How to Avoid the "Divide by Zero" Error in SQL by understanding various methods with the help of examples and so on.
How to Avoid the Divide by ZeroError in SQL?
In SQL, performing division operations can sometimes lead to errors, particularly when a divisor is zero. This error, often referred to as the “divide by zero” error can disrupt your query execution and lead to inaccurate results.
To avoid the divide by zero in SQL use these methods:
- Using NULLIF() function
- Using CASE statement
- Using SET ARITHABORT OFF
Let's setup an environment:
First, we will create a demo database, declare variables, and see how to counter SQL's "divide by zero" error message.
Query:
CREATE DATABASE Test;
DECLARE @Num1 INT;
DECLARE @Num2 INT;
SET @Num1=12;
SET @Num2=0;
1. Using NULLIF() function
If both arguments are equal, NULLIF() function returns NULL. If both arguments are not equal, it returns the value of the first argument.
Syntax:
NULLIF(exp1, exp2);
Now we are using the NULLIF() function in the denominator with the second argument value zero.
SELECT @Num1/NULLIF(@Num2,0) AS Division;
- In the SQL server, if we divide any number with a NULL value its output will be NULL.
- If the first argument is zero, it means if the Num2 value is zero, then NULLIF() function returns the NULL value.
- If the first argument is not zero, then NULLIF() function returns the value of that argument. And the division takes place as regular.
Output:

2. Using CASE statement
The SQL CASE statement is used to check the condition and return a value. It checks the conditions until it is true and if no conditions are true it returns the value in the else part.
We have to check the value of the denominator i.e the value of the Num2 variable. If it is zero then return NULL otherwise return the regular division.
SELECT CASE
WHEN @Num2=0
THEN NULL
ELSE @Num1/@Num2
END AS Division;
Output:

3. Using SET ARITHABORT OFF
To control the behavior of queries, we can use SET methods. By default, ARITHABORT is set as ON. It terminates the query and returns an error message. If we set it OFF it will terminate and returns a NULL value.
Like ARITHBORT, we have to set ANSI_WARNINGS OFF to avoid the error message.
SET ARITHABORT OFF;
SET ANSI_WARNINGS OFF;
SELECT @num1/@Num2;
Output:
Conclusion
Handling the "divide by zero" error in SQL is crucial to ensure smooth and accurate query execution. By using techniques such as the NULLIF()
function, the CASE
statement, and the SET ARITHABORT OFF
command, you can prevent this error from disrupting your operations. Each of these methods offers a different approach, allowing you to choose the one that best fits your needs.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read