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 Friday, October 5, 2012 7:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:40 AM
Points: 5,916, Visits: 8,166
kapil190588 (10/5/2012)
hi,
I am a newbie in filed of DBa. Can anyone plz explain me how the 41,80 coming while executing--

DECLARE @nvarchar nvarchar(40)
DECLARE @nchar nchar(40)

SET @nvarchar = '7 chars'
SET @nchar = '7 chars'

SELECT len(@nvarchar), len(@nchar),
len(@nvarchar+';'), len(@nchar+';'),
datalength(@nvarchar), datalength(@nchar)
Correct answer:
7,7,8,41,14,80

thanks a lot in advance

Yep.
@nchar has data type nchar(40). Nchar is fixed length, so after assigning '7 chars' to it, it will be padded out to 40 characters by adding 33 spaces at the end. The expression @nchar + ';' then adds a semicolon after the 33rd space (the 40th character) for a total of 41 characters. That's the explanation for the 41.
The result 80 is returned by datalength(@nchar). Remember that @nchar was padded to use all 40 characters. But the data type is nchar(40), not char(40). The difference is that nchar is able to store the full Unicode character set, at the expense of taking two bytes for each character. So to store 40 nchar characters, SQL Server uses 80 bytes.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1369013
Posted Friday, October 5, 2012 7:18 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, July 10, 2014 11:20 AM
Points: 1,888, Visits: 2,323
thanks a lot Hugo it clears to me now!!


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1369018
Posted Thursday, May 30, 2013 3:15 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, September 27, 2013 5:37 AM
Points: 306, Visits: 524
Got it right 7,7,8,41,14,80
but thought 7,7,8,8,14,80

but that was`t in ans


Neeraj Prasad Sharma
Sql Server Tutorials
Post #1458100
« Prev Topic | Next Topic »

Add to briefcase «««12345

Permissions Expand / Collapse