|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 3:00 AM
Points: 1,151,
Visits: 879
|
|
Rick Romack (9/10/2009) I do disagree with this answer, If I run DECLARE @str NVARCHAR(10) SET @str = '1, 2, 3, ' SET @str = LEFT(@str, LEN(@str)- 2) select len(@str) SELECT @str The return for len(@str) returns 5 that would be '1, 2,' with no space after the comma.
If I copy the result of the SELECT @str to word and have it show all characters there is no space after the comma.
I was about to say I agree with you Rick and I actually did until...... DECLARE @str NVARCHAR(10) SET @str = '1, 2, 3, ' SET @str = LEFT(@str, LEN(@str)- 2) select '"'+@str+'"' mystr, len(@str) lenstr
The result as copied from sql server 2005 and 2008. Both gave the same results. mystr lenstr "1, 2, " 5
When I pasted the result in here directly from ssms I saw that there was actually a space at the end but what stumps me is the fact that it gave a LEN of 5 characters and that does not include the space at the end. Can someone please clear this for me 'cause I am stumped.
Manie Verster Developer Johannesburg South Africa
Life is about choices.... I choose to be happy today
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 6:29 PM
Points: 387,
Visits: 681
|
|
Manie Verster (9/11/2009)
When I pasted the result in here directly from ssms I saw that there was actually a space at the end but what stumps me is the fact that it gave a LEN of 5 characters and that does not include the space at the end. Can someone please clear this for me 'cause I am stumped.
http://msdn.microsoft.com/en-us/library/ms190329.aspx
It's just the way MS decided to display length. It could be part of an ISO standard on how to report string lengths. If you wanted true length, do LEN(@str+'-')-1.
/* Anything is possible but is it worth it? */
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:16 AM
Points: 480,
Visits: 221
|
|
very interesting. I too thought the results would be "1, 2,". Until I set the output to text and realized I was wrong.
I thought the final select would "automatically" trim the trailing space since the field type was set to varchar. Again very interesting....
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 12:07 PM
Points: 104,
Visits: 296
|
|
| Also, LEN counts trailing tabs, but not spaces.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, November 10, 2010 5:12 AM
Points: 36,
Visits: 31
|
|
the given answer to this question is incorrect because the string variable contains 9 characters including the commas and spaces. Total length through first function being 9 - 2 = 7 and the first seven characters from the left is 1, 2, 3 which received more percentage of answers from the membership.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:16 AM
Points: 480,
Visits: 221
|
|
Adrian Charles Chetwynd-Talbot (9/17/2009)
the given answer to this question is incorrect because the string variable contains 9 characters including the commas and spaces. Total length through first function being 9 - 2 = 7 and the first seven characters from the left is 1, 2, 3 which received more percentage of answers from the membership. 
Keep in mind the data type nvarchar will remove the trailing space. So the length only starts with 8 characters rather then 9. The trick in this question was the trailing space was not removed after the second SET.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, July 25, 2012 9:04 PM
Points: 542,
Visits: 187
|
|
| didn't see option with space there...
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 5:04 AM
Points: 3,046,
Visits: 1,309
|
|
| Nice question - had the same "doh!" moment as lots of other people by the look of it!
|
|
|
|