Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Dealing with the could not allocate new page for database 'TEMPDB'. There are no more pages available in filegroup DEFAULT error messageIncluding an Aggregated Column's Related Values »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

The differences between LEN and DATALENGTH in SQL Server!
I have seen a bunch of questions about this recently and decided to do a little post to clear up the confusion.
First let's take a look what Books On Line has to say about these two functions

LEN
Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks.

DATALENGTH
Returns the number of bytes used to represent any expression.

So what does that mean? It means that the LEN function will first right trim the value and then give you a count of the charaters, the DATALENGTH function on the other hand does not right trim the value and gives you the storage space required for the characters.

Take a look at this example

  1. DECLARE @v NCHAR(5)
  2. SELECT @v ='ABC  '
  3.  
  4.  
  5. SELECT LEN(@v),datalength(@v)

The output for len is 3 while the output for datalength =10. The reason that datalength returns the value 10 is because nvarchar uses 2 bytes to store 1 character by using unicode while varchar is using ascii which requires 1 byte per charaters

Let's take a look at some more data, first create this table

  1. CREATE TABLE #TeslLen ( CharCol CHAR(5),
  2.             VarCharCol VARCHAR(5),
  3.             NCharCol NCHAR(5),
  4.             NVarCharCol NVARCHAR(5))
  5.  
  6.  
  7. INSERT #TeslLen VALUES('A','A','A','A')
  8. INSERT #TeslLen VALUES('AB','AB','AB','AB')
  9. INSERT #TeslLen VALUES('ABC','ABC','ABC','ABC')
  10. INSERT #TeslLen VALUES('ABCD','ABCD','ABCD','ABCD')
  11. INSERT #TeslLen VALUES('ABCDE','ABCDE','ABCDE','ABCDE')
  12. INSERT #TeslLen VALUES(' ',' ',' ',' ')

Now run the following query

  1. SELECT CharCol AS VALUE,LEN(CharCol) AS LenChar,DATALENGTH(CharCol) AS DLenChar,
  2.     LEN(VarCharCol) AS LenVarChar,DATALENGTH(VarCharCol)AS DLenVarChar,
  3.     LEN(NCharCol) AS LenNChar,DATALENGTH(NCharCol) AS DLenNChar,
  4.     LEN(NVarCharCol) AS LenNVarChar,DATALENGTH(NVarCharCol) AS DLenNVarChar
  5. FROM #TeslLen

Here is the output for all the columns with LEN and DATALENGTH

Value LenChar DatalengthChar LenVarChar DatalengthVarChar LenNChar DatalengthNChar LenNVarChar DatalengthNVarChar
A 1 5 1 1 1 10 1 2
AB 2 5 2 2 2 10 2 4
ABC 3 5 3 3 3 10 3 6
ABCD 4 5 4 4 4 10 4 8
ABCDE 5 5 5 5 5 10 5 10
0 5 0 1 0 10 0 2




*** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1989 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

4 comments

Comment from: George Mastros [Member] Email
*****
Excellent post. Another difference between Len and DataLength is that Len does not work for Text and nText columns, but DataLength does.

This is important for us "poor folk" stuck with SQL 2000.
07/20/09 @ 16:59
Comment from: SQLDenis [Member] Email
text? what is that :-)

I am glad to say that the last time I did use text/ntext/image/blob was in the year 2000 and haven't used it since :-)
07/20/09 @ 17:23
Comment from: anandkbs [Member] Email
****-
its an good post.
can u please specify Where exactly datalength is used.
07/21/09 @ 05:32
Comment from: SQLDenis [Member] Email
you would use datalength when you want to count spaces which are trimmed when using len, have to work with unicode data or have to work with text or ntext data
07/21/09 @ 06:52

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)