Home » SQL & PL/SQL » SQL & PL/SQL » how to handle zero divide error in formula calculation
how to handle zero divide error in formula calculation [message #626770] Sun, 02 November 2014 11:28 Go to next message
venki459
Messages: 5
Registered: November 2014
Location: CHENNAI
Junior Member
Procedure credit_Intrest is

v_tot   number;

v_res   number;

v_tp    number;

v_start_date   date;

v_end_date     date;


Begin

 v_res  := v_tot/v_tp*(v_start_date -v_end_date);
end;

End credit_Intrest;

---------------------------------------------------------
In the above calculation when v_start_date and v_end_date are same it is becoming zero.
so zero_divide is comming.

To use Nvl also it is not null both the dates are there.

could you pls suggest any solution to resolve.


Edited by Lalit : Added code tags

[Updated on: Sun, 02 November 2014 11:46] by Moderator

Report message to a moderator

Re: how to handle zero divide error in formula calculation [message #626771 is a reply to message #626770] Sun, 02 November 2014 11:44 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and please read How to use [code] tags and make your code easier to read

What result would you want when both dates are the same?
Re: how to handle zero divide error in formula calculation [message #626772 is a reply to message #626770] Sun, 02 November 2014 11:44 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
So what do you want to do if the two values are equal and produce a zero divisor?
Put an IF statement in your procedure to test it, and process accordingly.
Re: how to handle zero divide error in formula calculation [message #626773 is a reply to message #626770] Sun, 02 November 2014 11:45 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Welcome to the forum!

Please read and follow the forum guidelines, to enable us to help you: http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/
Re: how to handle zero divide error in formula calculation [message #626774 is a reply to message #626770] Sun, 02 November 2014 12:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Database PL/SQL Language Reference
Chapter 11 PL/SQL Error Handling
Section Predefined Exceptions

Re: how to handle zero divide error in formula calculation [message #626775 is a reply to message #626770] Sun, 02 November 2014 12:26 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
You could also add an exception block to handle the zero_divide. It looks like you are calculating interest, based on number of days, so presumably if there are zero days, then there is zero interest. So, you could do something like below. I assume that this is just a partial procedure that you are working on, since although it may calculate something it does not output the result in any way yet.

CREATE OR REPLACE
Procedure credit_Intrest 
is
  v_tot          number;
  v_res          number;
  v_tp           number;
  v_start_date   date;
  v_end_date     date;
Begin
  v_res  := v_tot/v_tp*(v_start_date -v_end_date);
EXCEPTION
  WHEN ZERO_DIVIDE THEN v_res := 0;
End credit_Intrest;
/

Re: how to handle zero divide error in formula calculation [message #626785 is a reply to message #626775] Sun, 02 November 2014 21:33 Go to previous messageGo to next message
venki459
Messages: 5
Registered: November 2014
Location: CHENNAI
Junior Member
CREATE OR REPLACE
Procedure credit_Intrest
is
v_tot number;
v_res number;
v_tp number;
v_start_date date;
v_end_date date;
Begin
v_res := v_tot/v_tp*(v_start_date -v_end_date);
EXCEPTION
WHEN ZERO_DIVIDE THEN v_res := 0;
End credit_Intrest;

i Will add the Exception section, but it should not go to the exception Section.
when both dates are same it will give zero, to avoid zero divide error, any treatment to become the zero value to 1
Re: how to handle zero divide error in formula calculation [message #626786 is a reply to message #626785] Sun, 02 November 2014 21:52 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
venki459 wrote on Mon, 03 November 2014 09:03
any treatment to become the zero value to 1


Use NVL.

Did you try any of the suggestions provided above? What about the IF condition?
Re: how to handle zero divide error in formula calculation [message #626787 is a reply to message #626786] Sun, 02 November 2014 22:22 Go to previous messageGo to next message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
how about something like this (I have not checked the syntax for correctness, that is your job):


v_res := case when (v_start_date - v_end_date) = 0 then 0 else v_tot/v_tp*(v_start_date -v_end_date) end;
Re: how to handle zero divide error in formula calculation [message #626792 is a reply to message #626785] Sun, 02 November 2014 23:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
i Will add the Exception section, but it should not go to the exception Section.
when both dates are same it will give zero, to avoid zero divide error, any treatment to become the zero value to 1


You write what you want in the exception section.

Re: how to handle zero divide error in formula calculation [message #626801 is a reply to message #626785] Mon, 03 November 2014 02:20 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
you may use decode, if the v_start_date and v_end_date are same then you can replave the the 0 with something else you would like to. can you explain what is this calculation doing?
v_res := v_tot/v_tp*decode((v_start_date -v_end_date),0,<value according to Business logic>,(v_start_date -v_end_date));


Or alternatively : assuming Dates are without time componanernts
v_res := v_tot/v_tp*decode(v_start_date,v_end_date,<value according to Business logic>,(v_start_date -v_end_date));

[Updated on: Mon, 03 November 2014 02:23]

Report message to a moderator

Re: how to handle zero divide error in formula calculation [message #626803 is a reply to message #626801] Mon, 03 November 2014 02:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You cannot use DECODE in a PL/SQL expression:
SQL> declare a int := 1; b int := 1; c int;
  2  begin
  3    c := decode(a,1,1,0)+b;
  4  end;
  5  /
  c := decode(a,1,1,0)+b;
       *
ERROR at line 3:
ORA-06550: line 3, column 8:
PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
ORA-06550: line 3, column 3:
PL/SQL: Statement ignored

And CASE has already been suggested by Kevin.

However to embed an exceptional case in a a formula is not the best way to program and make your code clear and maintanable.
An EXCEPTION block is a better alternative. There we see what the code does and why it does it.
Just my opinion.

Re: how to handle zero divide error in formula calculation [message #626804 is a reply to message #626803] Mon, 03 November 2014 02:39 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
Sorry, I should have tested it before post. Sad
Re: how to handle zero divide error in formula calculation [message #626807 is a reply to message #626804] Mon, 03 November 2014 02:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Everyone felt in this trap one day. Smile

Re: how to handle zero divide error in formula calculation [message #626899 is a reply to message #626785] Tue, 04 November 2014 22:23 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
i tried testing the procedure using simple if condition and it worked with both scenarios of same dates and different dates.


--with same dates

SQL> SET SERVEROUTPUT ON
SQL> declare
  2     v_tot          NUMBER;
  3     v_res          NUMBER;
  4     v_tp           NUMBER;
  5     v_start_date   DATE := to_date('01-SEP-2014');
  6     v_end_date     DATE := TO_date('01-SEP-2014');
  7  BEGIN
  8     IF v_start_date = v_end_date
  9     THEN
 10        v_res := 0;
 11         ELSE
 12        v_res := v_tot / v_tp * (v_start_date - v_end_date);
 13     END IF;
 14    DBMS_OUTPUT.PUT_LINE ('Value of v_res'||'-'||to_char(v_res));
 15  EXCEPTION
 16     WHEN ZERO_DIVIDE
 17     THEN
 18        v_res := 0;
 19  END credit_intrest;
 20  /
Value of v_res-0

PL/SQL procedure successfully completed.


--with different dates.

  1  declare
  2     v_tot          NUMBER := 40;
  3     v_res          NUMBER ;
  4     v_tp           NUMBER := 2;
  5     v_start_date   DATE := to_date('30-SEP-2014');
  6     v_end_date     DATE := TO_date('01-SEP-2014');
  7  BEGIN
  8     IF v_start_date = v_end_date
  9     THEN
 10        v_res := 0;
 11         ELSE
 12        v_res := v_tot / v_tp * (v_start_date - v_end_date);
 13     END IF;
 14    DBMS_OUTPUT.PUT_LINE ('Value of v_res'||'-'||to_char(v_res));
 15  EXCEPTION
 16     WHEN ZERO_DIVIDE
 17     THEN
 18        v_res := 0;
 19* END credit_intrest;
SQL> /
Value of v_res-580

PL/SQL procedure successfully completed.




Re: how to handle zero divide error in formula calculation [message #626901 is a reply to message #626899] Wed, 05 November 2014 00:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that:
v_res := v_tot / v_tp * (v_start_date - v_end_date);
is
v_res := (v_tot / v_tp) * (v_start_date - v_end_date);
and not
v_res := v_tot / (v_tp * (v_start_date - v_end_date));

And so you can't get a ZERO_DIVIDE exception with same dates, you just have 0.

[Updated on: Wed, 05 November 2014 01:20]

Report message to a moderator

icon14.gif  Re: how to handle zero divide error in formula calculation [message #626903 is a reply to message #626901] Wed, 05 November 2014 01:08 Go to previous message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
nice Michael.
Previous Topic: Data migration
Next Topic: trigger on update
Goto Forum:
  


Current Time: Thu Apr 25 12:37:53 CDT 2024