Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Nov 3, 2022, 2:54 AM EDT
https://www.c-sharpcorner.com/blogs/difference-between-len-and-datalength-function-in-sql
Mukesh NailwalRank #793 · 2.9K reputationPosted Nov 3, 2022, 4:17 AM EDT
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
Rijwan AnsariRank #82 · 24.2K reputationPosted Nov 3, 2022, 10:10 PM EDT
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 )
Amit MohantyRank #112 · 20K reputationPosted Nov 3, 2022, 12:25 AM EDT
LEN : The LEN function returns the number of characters in a variable. It also removes the trailing spaces and then return the length.
Example:
DECLARE @Name VARCHAR(20)='Amit' DECLARE @NameWithSpace VARCHAR(20)='Amit ' SELECT LEN(@Name) as [Length], LEN(@NameWithSpace) AS [LenghtWithSpace]Result: Both return 4.
DATALENGTH : This function returns the number of bytes occupy in a variable. It also considered the spaces also.
Example:
DECLARE @Name VARCHAR(20)='Amit' DECLARE @NameWithSpace VARCHAR(20)='Amit ' SELECT DATALENGTH(@Name) as [DLength], DATALENGTH(@NameWithSpace) AS [DLenghtWithSpace]Here DataLength of 1st variable is 4 but 2nd one is 5.
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 3, 2022, 7:50 AM EDT
Hi Naresh,
LEN : Return number of characters in variable. It will trim trailing space not leading space DECLARE @Name VARCHAR(20)='c-sharpcorner' SELECT LEN(@Name) as [len] -- out put : 13 DECLARE @Name VARCHAR(20)='c-sharpcorner ' SELECT LEN(@Name) as [len] -- out put : 13 DECLARE @Name VARCHAR(20)=' c-sharpcorner' SELECT LEN(@Name) as [len] -- out put : 15 DATALENGTH : returns the number of bytes occupy in a variable. It also considered the spaces also. DECLARE @Name1 VARCHAR(20)='c-sharpcorner ' SELECT DATALENGTH(@Name1) as [DataLength] --Varchar out put : 14 DECLARE @Name1 NVARCHAR(20)='c-sharpcorner ' SELECT DATALENGTH(@Name1) as [DataLength] --Nvarchar out put : 28Vishal YelveRank #178 · 13.6K reputationPosted Nov 3, 2022, 10:08 AM EDT
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
Satya KarkiRank #78 · 24.6K reputationPosted Nov 3, 2022, 10:15 PM EDT
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/
Brahma Prakash ShuklaRank #633 · 3.8K reputationPosted Nov 6, 2022, 2:04 AM EST
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
Rajeev KumarRank #1,334 · 1.3K reputationPosted Mar 14, 2023, 3:34 AM EDT
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 .
Sandhiya PriyaRank #26 · 43.2K reputationPosted Jan 2, 2026, 6:54 AM EST
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.
SELECT LEN('SQL Server ') AS Result;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.
SELECT DATALENGTH('SQL Server ') AS Result;Output:
Counts bytes
Includes trailing spaces
Works for strings, binary data, images, etc.
Key Differences
Unicode Example (NVARCHAR)
SELECT LEN(N'Hello') AS LenResult, DATALENGTH(N'Hello') AS DataLengthResult;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)