Difference between Len() and DataLength()
Loading
Difference between Len() and DataLength()
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandhiya PriyaPosted Jan 2, 2026, 11:54 AM
Difference between
LEN()andDATALENGTH()in SQL ServerBoth
LEN()andDATALENGTH()are used to measure length, but they serve different purposes.1.
LEN()Returns the number of characters, excluding trailing spaces.
Output:
Counts characters
Ignores trailing spaces
Works with string data types
2.
DATALENGTH()Returns the number of bytes used to store the value, including trailing spaces.
Output:
Counts bytes
Includes trailing spaces
Works for strings, binary data, images, etc.
Key Differences
Unicode Example (NVARCHAR)
Output:
?Because NVARCHAR uses 2 bytes per character
When to use which?
Use LEN() when you need the visible character count
Use DATALENGTH() when you need actual storage size or need to detect trailing spaces
Final Summary
LEN()? character count (no trailing spaces)DATALENGTH()? byte count (includes trailing spaces)Rajeev KumarPosted Mar 14, 2023, 7:34 AM
LEN() Returns the number of characters of the specified string expression, excluding trailing blanks.DATALENGTH() to return the size in bytes for a given string.It show size in Bytes with space .
Brahma Prakash ShuklaPosted Nov 6, 2022, 7:04 AM
LEN() Returns the number of characters of the specified string expression, excluding trailing blanks. DATALENGTH() Returns the number of bytes used to represent any expression
Satya KarkiPosted Nov 4, 2022, 2:15 AM
LEN() Returns the number of characters of the specified string expression, excluding trailing blanks. DATALENGTH() Returns the number of bytes used to represent any expression.
https://database.guide/len-vs-datalength-in-sql-server/
Rijwan AnsariPosted Nov 4, 2022, 2:10 AM
Use the LEN to return the number of characters encoded into a given string expression, and DATALENGTH to return the size in bytes for a given string expression. These outputs may differ depending on the data type and type of encoding used in the column.
Syntax for Len
LEN ( string_expression )
Syntax for Datalength
DATALENGTH ( expression )
Vishal YelvePosted Nov 3, 2022, 2:08 PM
SELECTLEN('Shree')AS'LEN'RESULT:

SELECTDATALENGTH('Shree')AS'DATALENGTH'RESULT:

SELECTLEN('Shree ')AS'LEN'RESULT:

SELECTDATALENGTH('Shree ')AS'DATALENGTH'RESULT:

EXAMPLE:
SELECTLEN(N'Shree')AS'LEN of Unicode chars'RESULT:

Note: The prefix N for any character string denotes that the following charcter string is of Unicode or double byte type
EXAMPLE:
SELECTDATALENGTH(N'Shree')AS'DATALENGTH of Unicode chars'RESULT:

DECLARE@NameNVARCHAR(50)='Shree'SELECTLEN(@Name)AS'LEN of Unicode chars'RESULT:

DECLARE@NameNVARCHAR(50)='Shree'SELECTDATALENGTH(@Name)AS'DATALENGTH of Unicode chars'RESULT:

DECLARE@NameNVARCHAR(50)='Shree 'SELECTLEN(@Name)AS'LEN of Unicode chars'RESULT:

DECLARE@NameNVARCHAR(50)='Shree 'SELECTDATALENGTH(@Name)AS'DATALENGTH of Unicode chars'RESULT:

DECLARE@CustomerTABLE(TextColumn TEXT)INSERTINTO@CustomerVALUES('100')SELECTLEN(TextColumn)AS'LEN'FROM#t1RESULT:
Msg 8116, Level 16, State 1, Line 7
Argument data type text is invalid for argument 1 of len function.
DECLARE@CustomerTABLE(TextColumn TEXT)INSERTINTO@CustomerVALUES('100')SELECTDATALENGTH(textcolumn)AS'DATALENGTH'FROM#t1RESULT:
DATALENGTH
———–
3
Jignesh KumarPosted Nov 3, 2022, 11:50 AM
Hi Naresh,
Mukesh NailwalPosted Nov 3, 2022, 8:17 AM
Hello @Naresh Kumar,
Please refer https://www.c-sharpcorner.com/blogs/difference-between-len-and-datalength-function-in-sql1 to know the difference between Len() and DataLength()
Thanks
Rajanikant HawaldarPosted Nov 3, 2022, 6:54 AM
https://www.c-sharpcorner.com/blogs/difference-between-len-and-datalength-function-in-sql
Amit MohantyPosted Nov 3, 2022, 4:25 AM
LEN : The LEN function returns the number of characters in a variable. It also removes the trailing spaces and then return the length.
Example:
Result: Both return 4.
DATALENGTH : This function returns the number of bytes occupy in a variable. It also considered the spaces also.
Example:
Here DataLength of 1st variable is 4 but 2nd one is 5.