• 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:

    DECLARE @y VARCHAR(10)

    SET @y = ''

    SELECT NullIf(@y,0)/0 as Q3

    /*Returns:

    Q3

    -----------

    NULL

    (1 row(s) affected)

    */

    The trouble is, you'll now get a NULL result even if the divisor is not a zero:

    DECLARE @y VARCHAR(10)

    SET @y = ''

    SELECT NullIf(@y,0)/353 as Q3

    /* Also returns...

    Q3

    -----------

    NULL

    (1 row(s) affected)

    If the dividend is not zero (or implicitly converted to zero), the divide-by error is not caught:DECLARE @x VARCHAR(10), @y VARCHAR(10)

    SET @y = '5'

    SELECT NullIf(@y,0)/0 as Q3

    /* Returns:

    Q3

    -----------

    Msg 8134, Level 16, State 1, Line 3

    Divide by zero error encountered.

    */

    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:DECLARE @y VARCHAR(10)

    SET @y = ''

    SELECT 50/NULLIF(@y,0) as Q3

    /*Returns:

    Q3

    -----------

    NULL

    (1 row(s) affected)

    */

    But this will not:

    DECLARE @y VARCHAR(10)

    SET @y = '5.0'

    SELECT 50/NULLIF(@y,0) as Q3

    /*Returns:

    Q3

    -----------

    Msg 245, Level 16, State 1, Line 3

    Conversion failed when converting the varchar value '5.0' to data type int.

    */

    If we try to get around that by making all the numbers DECIMAL, the conversion still fails:DECLARE @y VARCHAR(10)

    SET @y = '5.0'

    SELECT 50.0/NULLIF(@y,0.0) as Q3

    /*

    Q3

    ---------------------------------------

    Msg 8115, Level 16, State 8, Line 3

    Arithmetic overflow error converting varchar to data type numeric.

    */

    Finally, in regards to your question....

    Again with respect, NULLIF() would eliminate the error for query 4 (as it would for query 3):

    DECLARE @y VARCHAR(10)

    SET @y = ''

    SELECT NULLIF(@y,0)/2.0 as Q4b -- query 4b

    SELECT NULLIF(@y,0)/0.0 as Q3b -- query 3b

    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.