-4

I am trying to double divide but I am getting no results.

The following is an example:

6.82 / (X/NULLIF (Y,0))
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
  • To improve the quality of your Question, please identify which form of `SQL` you are using (such as `MySQL`, `SQL-Server`, etc.) with another tag on your question. – toonice May 08 '17 at 02:56

2 Answers2

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
0

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

toonice
  • 2,211
  • 1
  • 13
  • 20
  • Hi, Thanks for the answer, but i have tried the equation - X / NULLIF (Y,0) and that is giving me results and thus x is not zero at all, the no results happen when i want to perform the mentioned equation. Is the double division possible at all? – Akshay Mehta May 08 '17 at 13:05
  • I would be very surprised if it is not. That sort of operation is fairly basic by maths standards, and should be in the capabilities of most languages. I can't say for certain, though, as you have not identified which language you are using (as per my Comment on the Question). If you can tell me that I can perform some research and hopefully figure this out. – toonice May 08 '17 at 13:22
  • Thanks for your help its MySql, sorry first time here. – Akshay Mehta May 08 '17 at 15:42
  • No worries. I'll look into it tomorrow morning (it's 2346 at the moment). In the meantime I suggest editing your Question to add a *tag* for `MySQL`. – toonice May 08 '17 at 15:46
  • Pls let me know if you were able to get any sense out of my equation :) – Akshay Mehta May 09 '17 at 12:22
  • I have updated my Answer so that your formula now uses the `COALESCE()` function. I recommend you read the article on the function I have included a link to. Please feel free to get back to me with any questions. – toonice May 09 '17 at 13:22