-- Correct your code so that you do not divide a number by zero.
To calculate percentage between field1 and field 2
1 | SELECT ((FIELD1/FIELD2) *100) as Percentage from TableA; |
1 2 | ERROR at line 1: ORA-01476: divisor is equal to zero |
1. The first and foremost way is to enforce the business logic and try to ensure that the field doesn’t contain a 0 in the first place.
2. Use the DECODE function
1 | DECODE(FIELD2,0,0,((FIELD1/FIELD2)*100)) |
This will return 0 in case the divisor is set to 03.User ZERO_DIVIDE to handle a zero divisor error In PL/SQL you can trap the error using ZERO_DIVIDE option. The best way to do it is replace the zero with a very small value like 0.00001
|
No comments:
Post a Comment