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

    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

    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 SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    8277 views
    InstapaperVote on HN

    4 comments

    Comment from: George Mastros (gmmastros) [Member]
    *****
    George Mastros (gmmastros) 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
    SQLDenis 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
    ****-
    anandkbs its an good post.
    can u please specify Where exactly datalength is used.
    07/21/09 @ 05:32
    Comment from: SQLDenis [Member] Email
    SQLDenis 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.

    To mislead the spambots.

    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.)