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 ««12345»»»

String Length Expand / Collapse
Author
Message
Posted Wednesday, July 11, 2012 3:13 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:39 AM
Points: 1,639, Visits: 5,619
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...
Post #1328129
Posted Wednesday, July 11, 2012 4:17 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Thursday, June 12, 2014 4:19 AM
Points: 701, Visits: 1,145
Great question. Tests some basics that sometimes trips us up.
Post #1328161
Posted Wednesday, July 11, 2012 5:09 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 1:58 AM
Points: 1,150, Visits: 1,088
Nice one. I learned some new stuff today.
Post #1328174
Posted Wednesday, July 11, 2012 6:21 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, September 12, 2014 10:23 AM
Points: 410, Visits: 366
Why does datalength of nchar(40) give 80?!?? That really threw me.

Thanks!
Post #1328213
Posted Wednesday, July 11, 2012 6:25 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:39 AM
Points: 1,639, Visits: 5,619
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.
Post #1328217
Posted Wednesday, July 11, 2012 6:27 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: Thursday, September 11, 2014 8:33 AM
Points: 3,313, Visits: 1,984
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.
Post #1328221
Posted Wednesday, July 11, 2012 6:57 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 7:53 AM
Points: 1,878, Visits: 1,412
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.



Everything is awesome!
Post #1328237
Posted Wednesday, July 11, 2012 7:02 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 27, 2013 4:28 AM
Points: 1,248, Visits: 779
Great question Kenneth, refreshed the old memory banks there

_____________________________________________________________________
"The difficult tasks we do immediately, the impossible takes a little longer"
Post #1328241
Posted Wednesday, July 11, 2012 7:20 AM


SSC-Insane

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

Group: General Forum Members
Last Login: Today @ 2:50 PM
Points: 21,744, Visits: 15,435
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 #1328260
Posted Wednesday, July 11, 2012 7:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 1,315, Visits: 2,541
Good question!


Sujeet Singh
Post #1328261
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse