Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

ORA-01476: divisor is equal to zero

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
  • User AvatarKiran Dalvi
  • 11 Nov, 2021
  • 0 Comments
  • 1 Min Read

ORA-01476: divisor is equal to zero

ORA-01476: divisor is equal to zero

ORA-01476 is a very general error, and it comes when we try to divide any number by 0. In mathematics, DIVIDEND/0 has no meaning. Or we can simply say division by zero is undefined. In Oracle database when we try to divide by 0, Oracle Database throws an exception - "ORA-01476: divisor is equal to zero". For Example:

[php] ankush@thavali> WITH T AS ( 2 SELECT 1 DIVIDEND, 0 DIVISOR FROM DUAL 3 ) 4 SELECT DIVIDEND/DIVISOR AS QUOTIENT FROM T; SELECT DIVIDEND/DIVISOR AS QUOTIENT FROM T * ERROR at line 4: ORA-01476: divisor is equal to zero [php]

Now the question is how to properly handle ORA-01476 properly in SQL, and return NULL (undefined) in such cases. I use following very simple methods to avoid ORA-01476

1. Case When Then

Here we simply return NULL is DIVISOR is ZERO, otherwise we divide.

1
2
3
4
5
6
7
8
9
10
11
12
ankush@thavali> WITH T AS (
  2   SELECT 1 DIVIDEND, 0 DIVISOR FROM DUAL
  3  )
  4  SELECT
  5     CASE WHEN DIVISOR = 0
  6     THEN NULL
  7     ELSE DIVIDEND/DIVISOR
  8     END QUOTIENT
  9  FROM T;
 
  QUOTIENT
----------

2. NULLIF(expr1, expr2)

I prefer this over CASE WHEN THEN approach

NULLIF compares expr1 and expr2. If they are equal, then the function returns NULL. If they are not equal, then the function returns EXPR1. In Oracle Database any mathematical operation involving NULL is evaluated to NULL - DIVIDEND/NULL = NULL

1
2
3
4
5
6
7
8
9
ankush@thavali> WITH T AS (
2 SELECT 1 DIVIDEND, 0 DIVISOR FROM DUAL
3 )
4 SELECT
5 DIVIDEND/NULLIF(DIVISOR ,0) QUOTIENT
6 FROM T;
 
QUOTIENT
----------