11/30/10

dividing by zero

If you are dividing to a column or sum in your select statement, and there's a chance that the column or the sum result might be a zero an error is going to occur on your select statement; to prevent the error and get a null result you can do the following

select sum(x)/sum(y) from dual; ----- result an error if sum(y) is zero

select sum(x)/decode (sum(y),0,null,sum(y)) from dual --- result is null if sum(y) is zero.

By using the decode, in case of zero the null is going to replace the zero or otherwise take the sum. dividing by null is null.

No comments: