DATALENGTH (Transact-SQL)

**APPLIES TO:** ![yes](media/yes.png)SQL Server (starting with 2008) ![yes](media/yes.png)Azure SQL Database ![yes](media/yes.png)Azure SQL Data Warehouse ![yes](media/yes.png)Parallel Data Warehouse

This function returns the number of bytes used to represent any expression.

Topic link icon Transact-SQL Syntax Conventions

Syntax

DATALENGTH ( expression )   

Arguments

expression
An expression of any data type.

Return types

bigint if expression has an nvarchar(max), varbinary(max), or varchar(max) data type; otherwise int.

Remarks

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.

Examples

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  

See also

LEN (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
System Functions (Transact-SQL)