I am trying to double divide but I am getting no results.
The following is an example:
6.82 / (X/NULLIF (Y,0))
I am trying to double divide but I am getting no results.
The following is an example:
6.82 / (X/NULLIF (Y,0))
6.82 / (X / ISNULL(Y,0))
This will throw a divide by zero error when Y
is NULL
or 0
. You can introduce conditional logic to handle such error.
If Y
is NULL
or 0
then your denominator will equate to -
( X / 0 )
This should result in a division by 0
error.
This potential for error can be corrected by rewriting your overall expression as -
( 6.82 * COALESCE( Y, 0 ) ) / X
The COALESCE()
function shall return the first non-NULL
argument it encounters, thus in this case if Y
is null it will return 0
, otherwise it will return Y
.
Similarly, if X
is equal to 0
then a division by 0
error should occur. How you should avoid this depends on the context of this problem, which you have not identified. Thus I can not suggest how to handle this situation.
If you have any questions or comments, then please feel free to post a Comment accordingly.
Further Reading
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#function_coalesce (on MySQL's COALESCE()
function