how to handle zero divide error in formula calculation [message #626770] |
Sun, 02 November 2014 11:28 |
|
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 #626801 is a reply to message #626785] |
Mon, 03 November 2014 02:20 |
|
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 |
|
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.
|
|
|
|
|
|
|
|