SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Varchar division


Varchar division

Author
Message
John Deupree
John Deupree
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1003 Visits: 374
Comments posted to this topic are about the item Varchar division

John Deupree
Michael Poppers
Michael Poppers
SSC Eights!
SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)

Group: General Forum Members
Points: 829 Visits: 416
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


bradfultz
bradfultz
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 322
You really need to be more specific in your question, i.e. what connection settings are set, such as "SET ANSI_WARNINGS OFF"!
Steve Jones
Steve Jones
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: Administrators
Points: 62384 Visits: 19102
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
My Blog: www.voiceofthedba.com
john.arnott
john.arnott
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1968 Visits: 3059
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
Michael Poppers
Michael Poppers
SSC Eights!
SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)SSC Eights! (829 reputation)

Group: General Forum Members
Points: 829 Visits: 416
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!
bradfultz
bradfultz
SSC Rookie
SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)SSC Rookie (49 reputation)

Group: General Forum Members
Points: 49 Visits: 322
None of us should be paid to assume anything, especially since each client can individually set their connection settings.
john.arnott
john.arnott
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1968 Visits: 3059
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.
john.arnott
john.arnott
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1968 Visits: 3059
-------
Edit: Deleted duplicate post. (slow response from SSC timed out, so I tried again.)
Rick-153145
Rick-153145
SSC Veteran
SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)SSC Veteran (214 reputation)

Group: General Forum Members
Points: 214 Visits: 356
.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search