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

What will be output? Expand / Collapse
Author
Message
Posted Wednesday, March 24, 2010 9:02 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 18, 2013 6:40 AM
Points: 253, Visits: 79
Comments posted to this topic are about the item What will be output?
Post #889502
Posted Wednesday, March 24, 2010 9:03 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:59 PM
Points: 21,297, Visits: 14,988
Thanks for the Question.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #889503
Posted Wednesday, March 24, 2010 10:39 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, June 30, 2014 2:51 AM
Points: 1,976, Visits: 369
thanks ... its a good interview question.
Post #889532
Posted Thursday, March 25, 2010 1:30 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 3:51 AM
Points: 317, Visits: 57
Thanks good question . For info rmation if we write question like this

declare @a int
declare @b varchar(2)
set @a = 10
set @b = ' '+@a +' '+ 2+' '
select @a +' '+ @b

then also output will be same.
Post #889578
Posted Thursday, March 25, 2010 2:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:35 PM
Points: 5,930, Visits: 8,179
Good question, but unfortunately the explanation is not entirely correct. Especially this part is very misleading:
Also the preceding space is not considered while performing the operation.


Let's look in detail what happens to set @b = ' '+@a + 2. Since all operators are the same, evaluation order is left to right. So the first partial expression to evaluate is ' '+@a. This mixes two datatypes: ' ' is varchar(1), and @a is int. Order of precedence dictates conversion of varchar to int, and ' ' is converted to the value 0 (run SELECT CAST(' ' AS int); if you don't believe me). Since @a is set to 10, the result of this partial expression is 0 + 10, or 10; still types as int.

The next step is to evaluate (' '+@a) + 2 (parentheses added to emphasize evaluation order). We have already seen that (' '+@a) is equal to the integer value 10, so this is now reduced to 10 + 2.

As you have seen, there is no leading space to be considered anywhere. However, even if there was, the words "is not considered" is misleading. I think we would all agree that converting the values '5', ' 5' and ' 5 ' to integer should all result in the value 5 - not because we expect the leading and trailing spaces to be "not considered", but because considering them or not is irrelevant, as they have no impact on the interpretation of the string as a number.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #889588
Posted Thursday, March 25, 2010 2:04 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:35 PM
Points: 5,930, Visits: 8,179
ziangij (3/24/2010)
thanks ... its a good interview question.
I would not be too happy with these things being asked in an interview. My answer would be that mixing data types like this and taking a dependency hit on implied evaluation order and implicit conversion rules should not be done in production code.

Code like this would get a red mark from me if I caught it in code review.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #889589
Posted Thursday, March 25, 2010 2:55 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 1:41 AM
Points: 614, Visits: 481
I would certainly agree with Hugo - this just smells like bad practice if it ends up in production code. OK, the question is fine as an academic exercise, but just because you can do something like this doesn't mean you should, IMHO.
btw - I got it wrong - probably because my style of coding doesn't give me enough practice at this kind of dubious T-SQL
Post #889611
Posted Thursday, March 25, 2010 3:23 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Hugo Kornelis (3/25/2010)
' ' is converted to the value 0 (run SELECT CAST(' ' AS int); if you don't believe me).

This is very interesting because ISNUMERIC(' ') returns 0.
On the one hand, SQL Server doesn't consider whitespaces as numeric values; on the other hand, it converts whitespaces to zero integer values. This is a little bit strange
Post #889627
Posted Thursday, March 25, 2010 4:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, March 7, 2012 6:57 AM
Points: 20, Visits: 168
The answer is 22
Post #889652
Posted Thursday, March 25, 2010 5:09 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, September 18, 2013 6:40 AM
Points: 253, Visits: 79
Thanks Hugo! your explaination is very helpful. also thanks for pointing out the misleading pharse. in future i can now be more careful for writing any stuff on SSC
Post #889666
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse