The LEN() and DATALENGTH() functions are there for completely different purpose. But it often confuses many in identifying which one to use when. The aim of this article is to clear all these confusions by presenting a comparative analysis of LEN() and DATALENGTH() Sql Serer functions.
[ALSO READ] Difference Between Sql Server VARCHAR and NVARCHAR Data Type
Below table lists out the comparative analysis between LEN() and DATALENGTH() Sql Server functions:
LEN() | DATALENGTH() |
DEFINITION | |
The LEN() Sql Server function returns the number of characters in the specified string expression | The DATALENGTH() Sql Server function returns the number of bytes used/required to represent an expression |
SYNTAX | |
LEN ( string_expression ) | DATALENGTH ( expression ) |
INPUT PARAMETER | |
Input parameter is a string expression, it can be a constant or variable or column of character or binary data | Input parameter is an expression of any data type |
RETURN TYPE | |
bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int | bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise int |
EXAMPLE | |
SELECT LEN('Shree') AS 'LEN' |
SELECT DATALENGTH('Shree') AS 'DATALENGTH' |
EXCLUDES TRAILING BLANK SPACES? | |
YES. LEN() function excludes the trailing blank spaces while calculating the number of characters in the specified string expressionEXAMPLE:
SELECT LEN('Shree ') AS 'LEN' |
NO. DATALENGTH() function includes the trailing blank spaces while calculating the number of bytes used/required to represent an expressionEXAMPLE:
SELECT DATALENGTH('Shree ') AS 'DATALENGTH' |
INPUT PARAMETER IS NULL? | |
LEN(NULL) is NULL |
DATALENGTH(NULL) is NULL |
WHEN INPUT IS A UNICODE (i.e. double byte) CHARACTER STRING CONSTANT | |
For LEN() function it doesn’t matter whether input is a single byte or double byte (i.e. unicode) charcter strings, it always counts the number of characters. EXAMPLE: SELECT LEN(N'Shree') AS 'LEN of Unicode chars' Note: The prefix N for any character string denotes that the following charcter string is of Unicode or double byte type |
As we know each character in the UNICODE character string requires two bytes of storage. So DATALENGTH of UNICODE character string will be equal to the number characters including spaces multiplied by 2. EXAMPLE: SELECT DATALENGTH(N'Shree') AS 'DATALENGTH of Unicode chars' |
WHEN INPUT IS A DOUBLE BYTE VARIABLE (EX: NVARCHAR DATATYPE) | |
DECLARE @Name NVARCHAR(50) = 'Shree' SELECT LEN(@Name) AS 'LEN of Unicode chars' |
DECLARE @Name NVARCHAR(50) = 'Shree' SELECT DATALENGTH(@Name) AS 'DATALENGTH of Unicode chars' |
WHEN INPUT IS A DOUBLE BYTE VARIABLE (EX: NVARCHAR DATATYPE) HAVING VALUE WITH TRAILING SPACES | |
DECLARE @Name NVARCHAR(50) = 'Shree ' SELECT LEN(@Name) AS 'LEN of Unicode chars' |
DECLARE @Name NVARCHAR(50) = 'Shree ' SELECT DATALENGTH(@Name) AS 'DATALENGTH of Unicode chars' |
SUPPORTS TEXT, NTEXT AND IMAGE DATA TYPES? | |
NO.
DECLARE @Customer TABLE (TextColumn TEXT) INSERT INTO @Customer VALUES('100') SELECT LEN(TextColumn) AS 'LEN' FROM #t1 RESULT: |
YES.
DECLARE @Customer TABLE (TextColumn TEXT) INSERT INTO @Customer VALUES('100') SELECT DATALENGTH(textcolumn) AS 'DATALENGTH' FROM #t1 RESULT: |
ALSO READ
- Varchar vs NVarchar
- Varchar vs Varchar(MAX)
- Char vs Varchar
- Text vs Varchar(Max)
- Union vs Union All
- DateTime vs DateTime2
- SET QUOTED_IDENTIFIER ON vs SET QUOTED_IDENTIFIER OFF
- Stored Procedure vs User Defined Function
- Primary Key vs Unique Key
- RAISERROR vs THROW
- Temporary Table vs Table Variable
- Len() vs Datalength()
- Sequence vs Identity
- DATEDIFF vs DATEDIFF_BIG
- LEFT JOIN vs LEFT OUTER JOIN
- RIGHT JOIN vs RIGHT OUTER JOIN
- JOIN vs INNER JOIN
- LEFT OUTER JOIN vs RIGHT OUTER JOIN
- SMALLDATETIME vs DATETIME