DATALENGTH (Transact-SQL)

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel 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

  • image
  • ntext
  • nvarchar
  • text
  • varbinary

and

  • varchar

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)