This function returns the number of bytes used to represent any expression.
Transact-SQL Syntax Conventions
expression
An expression of any data type.
bigint if expression has an nvarchar(max), varbinary(max), or varchar(max) data type; otherwise int.
DATALENGTH
becomes really helpful when used with
and
data types, because these data types can store variable-length data.
For a NULL value, DATALENGTH
returns NULL.
[!NOTE]
Compatibility levels can affect return values. See ALTER DATABASE Compatibility Level (Transact-SQL) for more information about compatibility levels.
This example finds the length of the Name
column in the Product
table:
-- Uses AdventureWorks
SELECT length = DATALENGTH(EnglishProductName), EnglishProductName
FROM dbo.DimProduct
ORDER BY EnglishProductName;
GO
LEN (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
System Functions (Transact-SQL)