Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Varchar division Expand / Collapse
Author
Message
Posted Saturday, September 12, 2009 2:57 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 3:02 PM
Points: 973, Visits: 293
Comments posted to this topic are about the item Varchar division


Post #786914
Posted Monday, September 14, 2009 8:31 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, March 04, 2014 7:41 PM
Points: 634, Visits: 399
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

Post #787504
Posted Monday, September 14, 2009 9:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 31, 2014 12:56 PM
Points: 43, Visits: 224
You really need to be more specific in your question, i.e. what connection settings are set, such as "SET ANSI_WARNINGS OFF"!
Post #787566
Posted Monday, September 14, 2009 10:04 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 11:24 AM
Points: 32,781, Visits: 14,942
If settings are not mentioned, assume the US English defaults.






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #787585
Posted Monday, September 14, 2009 5:09 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 8:55 AM
Points: 1,520, Visits: 3,035
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.
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
Post #787823
Posted Tuesday, September 15, 2009 10:43 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, March 04, 2014 7:41 PM
Points: 634, Visits: 399
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

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.
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!
Post #788360
Posted Tuesday, September 15, 2009 5:35 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, March 31, 2014 12:56 PM
Points: 43, Visits: 224
None of us should be paid to assume anything, especially since each client can individually set their connection settings.
Post #788634
Posted Tuesday, September 15, 2009 6:42 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 8:55 AM
Points: 1,520, Visits: 3,035
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.
Post #788649
Posted Tuesday, September 15, 2009 6:47 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 21, 2014 8:55 AM
Points: 1,520, Visits: 3,035
-------
Edit: Deleted duplicate post. (slow response from SSC timed out, so I tried again.)
Post #788651
Posted Tuesday, January 19, 2010 4:09 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 12:16 PM
Points: 178, Visits: 284
.
Post #849615
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse