|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:40 AM
Points: 1,272,
Visits: 4,310
|
|
Hugo Kornelis (7/11/2012) In my opinion, it is LEN() that has the strange behaviour here, because it returns the length of the padded version of the string instead of the length of the string itself.
Well, it's strange behaviour of LEN() when you provide it with an NCHAR parameter, which is what I sort of meant but failed to express in any comprehensible manner...
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Monday, June 17, 2013 6:18 AM
Points: 691,
Visits: 1,104
|
|
| Great question. Tests some basics that sometimes trips us up.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:51 AM
Points: 1,127,
Visits: 938
|
|
Nice one. I learned some new stuff today.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Yesterday @ 3:23 PM
Points: 355,
Visits: 309
|
|
Why does datalength of nchar(40) give 80?!?? That really threw me.
Thanks!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 1:40 AM
Points: 1,272,
Visits: 4,310
|
|
IowaDave (7/11/2012) Why does datalength of nchar(40) give 80?!?? That really threw me.
Thanks!
Because the "n" means the string is Unicode, so it has two bytes per character; DATALENGTH returns the actual length of the storage used for the field. 2 bytes per character * 40 characters = 80 bytes.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 11:48 AM
Points: 2,602,
Visits: 1,553
|
|
IowaDave (7/11/2012) Why does datalength of nchar(40) give 80?!?? That really threw me.
Thanks! Because it is the entire length of the string * 2 bytes. Nchar includes padding so the length is 40 versus 7 with nvarchar which does not include padding.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 8:24 AM
Points: 1,373,
Visits: 896
|
|
Thanks for the great question. I was actually brushing up on len() and datalength() just a couple of days ago so it was still really fresh on my brain.
Oh no, we're toast! I've got this. *Keyboard clatter* Woah, how'd you do that? I'm a DBA...Booyah
Yeah, uh huh, you know what it is. Everything I do, I do it big

|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 1:49 AM
Points: 1,182,
Visits: 745
|
|
Great question Kenneth, refreshed the old memory banks there
_____________________________________________________________________ "The difficult tasks we do immediately, the impossible takes a little longer"
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 3:33 PM
Points: 18,858,
Visits: 12,443
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 7:29 AM
Points: 1,084,
Visits: 1,954
|
|
Good question!
- Divine Flame
|
|
|
|