December 10, 2007 at 12:04 am
Hi Experts,
Len() - Will give the length of the character used,instead of length of the bit,excludes trailing spaces.
DataLength() - Will give the length of the bit used.
Apart from this, is there any difference ? i mean memory wise or storage wise or performance wise.
Experts inputs are welcome !
karthik
December 10, 2007 at 4:44 am
One difference is that NVARCHAR and VARCHAR store data differently. NVARCHAR uses two bytes per character vs. VARCHAR which stores it as one.
as in my example code ....
DECLARE @varChar VARCHAR(5)
DECLARE @nvarchar NVARCHAR(5)
SET @varChar = 'ABC'
SET @nvarchar = 'ABC'
SELECT
LEN(@varChar) AS varCharLength
,DATALENGTH(@varChar) AS varCharDataLength
,LEN(@NvarChar) AS varCharLength
,DATALENGTH(@NvarChar) AS varCharDataLength -- notice this length is 6 bytes
So the physical space reserved for NVARCHAR is double that of VARCHAR. This also applies to CHAR and NCHAR. Also, both VAR datatypes use an extra 2 bytes to store the length of the VAR data.
______________________________________________________________________
Personal Motto: Why push the envelope when you can just open it?
If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.
Jason L. SelburgDecember 10, 2007 at 4:55 am
DATALENGTH returns the length of the string in bytes, including trailing spaces. LEN returns the length in characters, excluding trailing spaces. For example,
SELECT
LEN('string'),
LEN('string '),
DATALENGTH('string'),
DATALENGTH('string '),
LEN(N'string'),
LEN(N'string '),
DATALENGTH(N'string'),
DATALENGTH(N'string ')
will return 6, 6, 6, 9, 6, 6, 12, 18
December 10, 2007 at 6:13 am
Datalength works on text and ntext data types where len does not.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 10, 2007 at 10:41 pm
Hi Vyas,
Can you tell me the difference between LEN('string') and Len(N'String') ?
Also for DataLength('string') and DataLength(N'string').
I am seeing this type of code first time.
Thanks in advance for your reply.
karthik
December 10, 2007 at 11:11 pm
The N signifies that the following value is unicode (nvarchar/nchar)
The LEN should return the same, the datalenght for the N-prefixed value should be twice that for the value without the N.
See above difference between datalength on varchar and narchar for the reason.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 12, 2007 at 1:09 am
Thanks Gila and Vyas.:)
karthik
December 12, 2007 at 1:15 am
Gila and Vyas,
Suppose i need to find the length of a string.In that situation which function should i use ?
For example,
Declare @STR varchar(35)
Select @STR = 'Welcome to Sqlservercentral.com'
which statement should i use ?
select len(@str)
(or)
select DataLength(@str)
Which statement would provide good performance ?
karthik
December 12, 2007 at 1:25 am
Depends what you want. I doubt there's much of a performance difference, if any.
Ifyou want the number of characters in the string, except trailing spaces, use LEN. If you what the storage size of the string, use Datalength
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 13, 2015 at 12:27 am
SELECT
LEN('string'),
LEN('string '),
DATALENGTH('string'),
DATALENGTH('string '),
LEN(N'string'),
LEN(N'string '),
DATALENGTH(N'string'),
DATALENGTH(N'string ')
will give values 6,6,6,7,6,6,12,14
January 13, 2015 at 1:43 am
Please note: 8 year old thread
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
February 16, 2015 at 7:18 am
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply