Thanks for the question! I didn't know that division by zero was technically OK in T-SQL in certain situations (like when the dividend is undefined). Since it is, one can avoid the query-3 problem with

Michael Poppers (9/14/2009)Thanks for the question! I didn't know that division by zero was technically OK in T-SQL in certain situations (like when the dividend is undefined). Since it is, one can avoid the query-3 problem with

SELECT NULLIF(@y, 0)/0 --query 3a

Hmmm.... Divide by zero "OK"??? Not at all. All you've done is force the return of NULL for the special case of the dividend @y being zero, taking advantage of the behavior that returns NULL for an expression when any part of the expression is NULL. Any other value of dividend @y will still cause a divide by zero error. Typically, the divide by zero error would occur with a variable divisor, so I suppose you could use this approach to return NULL rather than an error in those cases by wrapping the divisor (not the dividend) in a NullIf function.

Note, however, that although an empty string will implicitly convert to a zero value int, it will cause an error if the conversion is to numeric (or decimal). So, this NULLIF trick is probably not a great one to depend upon.

Michael Poppers (9/14/2009)...I didn't know that division by zero was technically OK in T-SQL in certain situations (like when the dividend is undefined). Since it is, one can avoid the query-3 problem with

SELECT NULLIF(@y, 0)/0 --query 3a

Hmmm.... Divide by zero "OK"??? Not at all....

With respect, Mr. Arnott, you misunderstood. I didn't say that a "divide by zero" situation was OK -- I said that the example in question was "technically OK," i.e. it would not return an error and stop the batch.

All you've done is force the return of NULL for the special case of the dividend @y being zero, taking advantage of the behavior that returns NULL for an expression when any part of the expression is NULL. Any other value of dividend @y will still cause a divide by zero error.

??? The situation in question involved a VARCHAR-type variable. I was simply suggesting that NULLIF() would eliminate the possibility of error (i.e. create a "technically OK" statement) for that data type. (The concept may very well work for other data types, too, but I have not spent time checking into that .)

Typically, the divide by zero error would occur with a variable divisor, so I suppose you could use this approach to return NULL rather than an error in those cases by wrapping the divisor (not the dividend) in a NullIf function.

Note, however, that although an empty string will implicitly convert to a zero value int, it will cause an error if the conversion is to numeric (or decimal). So, this NULLIF trick is probably not a great one to depend upon.

Oops. I'm sorry if I misunderstood. You did say "...division by zero was technically OK in T-SQL in certain situations...". The situation here is not that the divide by zero is OK, but that it is never attempted because one of the operands is seen to be NULL.

??? The situation in question involved a VARCHAR-type variable. I was simply suggesting that NULLIF() would eliminate the possibility of error (i.e. create a "technically OK" statement) for that data type. (The concept may very well work for other data types, too, but I have not spent time checking into that .)

The NULLIF() will indeed eliminate the possibility of error in that particular statement, but not because it's testing a VARCHAR. It works because the value of the dividend would be implicitly converted to a zero, and so forces the result to be NULL regardless of the value of divisor. To summarize...Your suggestion works to short-circuit the divide and return NULL:

Even if we then decide to use the NULLIF() on the divisor rather than the dividend in order to stop divide-by errors, we are then constraiined by by the inability of SQL Server to implicitly convert a string as a DECIMAL. So, this works:

The NULLIF() certainly returns NULL for either query, but query 4b would not have had an error if the division had been allowed to proceed. It's perfectly legitimate to divide zero by something else, the result always being zero. In this code snippet, we've lost the mathematically correct result for Q4b.

Michael, I thank you for the inspiration to explore this topic. I did learn a lot in doing so about implicit conversions and certainly about how a string datatype may be implicitly converted to an INT, but not a decimal, and also about the importance of being clear in my writing. I recognize now that my previous post was a bit confused.

Please don't think I was picking on you for your suggestion on using NULLIF(). On the contrary, I found it an interesting idea and wanted to see what its possibilities may have been. I think you'll agree that it turns out to be valuable mainly as a tool in understanding some of the behaviors of SQL Server, but probably would not do as a guard agains divide-by-zero in a practical application.