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


Why SQL Server converts '' to zero


Why SQL Server converts '' to zero

Author
Message
patrickbrewer
patrickbrewer
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 10
I have a web developer who sent me a query he couldn't figure out. Select Name from dbo.Employee where EmployeeID = ''. EmployeeID is an integer. First, I told him he shouldn't use '' as an integer. But, I could not explain why SQL Server converted '' to a zero. Can anyone answer that for me?

My assumption is that the engine sees a blank value as a zero because the column being compared to is an integer type. Since the value is nothing (not a null), the engine converts it to a zero because mathmatically zero added to anything doesn't change the value.
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32963 Visits: 16637
This indeed returns 0:
SELECT CAST('' AS int)


It's just the way it works. I suppose when the DBMS or the standards were being written, somebody decided that '' would convert to 0 rather than throwing an error.

John
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11722 Visits: 5478
John Mitchell-245523 (7/25/2013)
This indeed returns 0:
SELECT CAST('' AS int)


It's just the way it works. I suppose when the DBMS or the standards were being written, somebody decided that '' would convert to 0 rather than throwing an error.

John


Yeah, empty string, when converted to any T-SQL numeric datatypes except DECIMAL and NUMERIC will end up as 0 (zero).

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Alan.B
Alan.B
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12777 Visits: 7996
I asked this same question a few months back and got some good replies; just thought I'd share this:
http://www.sqlservercentral.com/Forums/Topic1397471-391-1.aspx

Check this out:

SELECT CAST(REPLICATE(CAST('' AS int),10) AS char(10)) AS [10 0's]
SELECT 22+$ AS [$22.00]



-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
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