|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, September 27, 2012 4:24 AM
Points: 253,
Visits: 78
|
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,528,
Visits: 359
|
|
thanks ... its a good interview question.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Friday, May 04, 2012 2:11 AM
Points: 313,
Visits: 56
|
|
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.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:30 PM
Points: 5,235,
Visits: 7,038
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:30 PM
Points: 5,235,
Visits: 7,038
|
|
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
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Today @ 8:52 AM
Points: 508,
Visits: 388
|
|
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
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 5:42 AM
Points: 3,189,
Visits: 4,146
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, March 07, 2012 6:57 AM
Points: 20,
Visits: 168
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, September 27, 2012 4:24 AM
Points: 253,
Visits: 78
|
|
| 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
|
|
|
|