Varchar division

  • Comments posted to this topic are about the item Varchar division

    John Deupree

  • 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

    [font="Courier New"]SELECT NULLIF(@y, 0)/0 --query 3a[/font]
  • You really need to be more specific in your question, i.e. what connection settings are set, such as "SET ANSI_WARNINGS OFF"!

  • If settings are not mentioned, assume the US English defaults.

  • 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

    [font="Courier New"]SELECT NULLIF(@y, 0)/0 --query 3a[/font]

    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.Declare @Dividend int

    Declare @Divisor int

    set @Dividend = 15

    set @Divisor = 0

    Select @Dividend / NullIf(@Divisor,0)

    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.

    DECLARE @x VARCHAR(10), @y VARCHAR(10)

    SET @y = ''

    SELECT @x/0.0 as Q1 -- query 1

    SELECT @x/2.0 as Q2 -- query 2

    SELECT @y/2.0 as Q4 -- query 4

    SELECT @y/0.0 as Q3 -- query 3

    This gives you a conversion error on query 4 (note that I moved it up in the list so you could see that it's not affected by the divide by zero).

    -----

    edit: added comments about implicit conversion to numeric or decimal

  • john.arnott (9/14/2009)


    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

    [font="Courier New"]SELECT NULLIF(@y, 0)/0 --query 3a[/font]

    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.Declare @Dividend int

    Declare @Divisor int

    set @Dividend = 15

    set @Divisor = 0

    Select @Dividend / NullIf(@Divisor,0)

    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.

    DECLARE @x VARCHAR(10), @y VARCHAR(10)

    SET @y = ''

    SELECT @x/0.0 as Q1 -- query 1

    SELECT @x/2.0 as Q2 -- query 2

    SELECT @y/2.0 as Q4 -- query 4

    SELECT @y/0.0 as Q3 -- query 3

    This gives you a conversion error on query 4 (note that I moved it up in the list so you could see that it's not affected by the divide by zero).

    -----

    edit: added comments about implicit conversion to numeric or decimal

    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

    Please explain what you're driving at. Thanks!

  • None of us should be paid to assume anything, especially since each client can individually set their connection settings.

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

  • -------

    Edit: Deleted duplicate post. (slow response from SSC timed out, so I tried again.)

  • .

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply