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 123»»»

How many rows? Expand / Collapse
Author
Message
Posted Thursday, August 26, 2010 9:00 PM


SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 7:16 AM
Points: 118, Visits: 174
Comments posted to this topic are about the item How many rows?

Prashant Bhatt
Sr Engineer - Application Programming
Post #976107
Posted Thursday, August 26, 2010 9:51 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 12:52 PM
Points: 611, Visits: 3,086
This really highlights why I don't like SQL Server's implicit data type conversion.


Post #976114
Posted Thursday, August 26, 2010 9:58 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,160, Visits: 2,191
Nice question, thanks!
Post #976116
Posted Thursday, August 26, 2010 10:16 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 25, 2014 11:09 PM
Points: 1,940, Visits: 1,173
but the same will not return any rows when we use

select * from #temp where code ='0'

this will return nothing.When we use 0 in quotes it will not return any no of rows.Implicits conversion happens from varchar to int.but it will not happen from int to varchar.We want to convert explicitly.

select * from #temp where code =convert(int,'0')

this will return three rows even we put zero in sigle quotes.


Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #976124
Posted Thursday, August 26, 2010 11:13 PM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 10:22 PM
Points: 1,457, Visits: 1,858
The essence of this question is in the following:

SELECT CONVERT(NVARCHAR(2),0)       -- returns 0
SELECT CONVERT(INT,N'') -- returns 0



Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #976135
Posted Thursday, August 26, 2010 11:45 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, November 2, 2014 11:03 PM
Points: 2,133, Visits: 371
good one... thanks
Post #976160
Posted Friday, August 27, 2010 12:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:28 AM
Points: 13,641, Visits: 11,517
Good question. It points out that you have to be very careful with implicit conversions.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #976164
Posted Friday, August 27, 2010 12:12 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 10:22 PM
Points: 1,457, Visits: 1,858
I am trying to find out "why" this behaviour is seen and believe that there is definitely something influencing this (I am just not able to find out what). It's not Data Type Precedence; but can be COLLATION Precedence.

Based on what I found in BOL, I developed the following small test:

SELECT CONVERT(NVARCHAR(2),0)       -- returns 0

-- returns 0, i.e. is NOT a Numeric value
SELECT ISNUMERIC('')

-- returns 0
SELECT CONVERT(INT,N'')
SELECT CONVERT(INT,N' ')
SELECT CAST(N' ' AS INT)
/*
Data type conversion (Database Engine):
"Character expressions that are being converted to an exact numeric data type must consist of digits, a decimal point, and an optional plus (+) or minus (-). Leading blanks are ignored. Comma separators, such as the thousands separator in 123,456.00, are not allowed in the string."
*/

-- returns Error
SELECT CONVERT(NUMERIC,N'')
/*
CAST & CONVERT :
"SQL Server also returns an error when an empty string (" ") is converted to numeric or decimal."
*/

If conversion to NUMERIC returns NULL, why does the conversion to INT succeed, and why does a blank string convert to 0?


Thanks & Regards,
Nakul Vachhrajani.
http://nakulvachhrajani.com
Be courteous. Drive responsibly.

Follow me on
Twitter: @sqltwins
Google Plus: +Nakul
Post #976167
Posted Friday, August 27, 2010 12:22 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, November 20, 2014 5:13 AM
Points: 1,131, Visits: 1,394
ISNUMERIC is returing 0 so implicit conversation is not converting BLANK to ZERO. Therefore the reason behind this is something else.

Thanks
Post #976173
Posted Friday, August 27, 2010 1:07 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 4:58 PM
Points: 6,133, Visits: 8,399
dogramone (8/26/2010)
This really highlights why I don't like SQL Server's implicit data type conversion.

Good point. Always ensure your types match, or use explicit conversion when they don't.
(Though the empty string also converts to 0 in explicit conversion)

malleswarareddy_m (8/26/2010)
but the same will not return any rows when we use

select * from #temp where code ='0'

this will return nothing.When we use 0 in quotes it will not return any no of rows.Implicits conversion happens from varchar to int.but it will not happen from int to varchar.We want to convert explicitly.

select * from #temp where code =convert(int,'0')

this will return three rows even we put zero in sigle quotes.

You are confusing some things. The first query doesn't do any conversion - both the column (code) and the constant ('0') are already of the character data type, so no conversion at all is required. And because the UPDATE has changed all the '0' strings to '' (empty) strings, no rows match.
In the second query, you are making it even more complicated. With the quotes, the constant is character; without them, it is integer. The explicit convert will convert it to integer (a no-op if you leave out the quotes). Then SQL Server will convert the contents of the code column to int for the comparison, again converting the empty string to the value 0. So the end effect will not be any different from
select * from #temp where code = 0;

You are right, though, that implicit conversion will not happen from integer to character. But for a different reason. Implicit conversions always follow the rules of data type precedence, and integer has a higher precedence. The only way to force conversion from integer to character is by using explicit conversion:
select * from #temp where code = convert(nvarchar(2), 0);
This will convert the integer value 0 to a character data type ('0'). The code column is also character, so no further conversion is required and the comparison is done. This is equivalent to using
select * from #temp where code = N''0';
No rows will be returned.

Nakul Vachhrajani (8/27/2010)
If conversion to NUMERIC returns NULL, why does the conversion to INT succeed, and why does a blank string convert to 0?

Answer to the first question: Because the rules for conversion to numeric are not the same as the rules for conversion to integer. And for conversion to float or money, yet other rules are used.
Speculative answer to the second question: The conversion algorithm must have some logic to deal with leading zeroes, as I expect both '00003' and '3' to convert to the same integer value. This same logic should also be used to consider '0000' and '0' as equal. It could be that the algorithm is too agressive, in allowing the last zero to be left out as well?
Or it could be by design. In my eyes, there are only two logical outcomes when converting an empty string to a numericall value: error or 0.

hardik.doshi (8/27/2010)
ISNUMERIC is returing 0 so implicit conversation is not converting BLANK to ZERO. Therefore the reason behind this is something else.

I consider this a bug in ISNUMERIC. According to Books Online, ISNUMERIC returns 1 if the "expression can be converted to at least one of the numeric types". Since the empty string can be converted to not one but several numeric types (int, money, and real), the return vallue should have been 1.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #976189
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse