Transact-SQL Reference


SQL Server 2008 Books Online (October 2009)
DATALENGTH (Transact-SQL)

Returns the number of bytes used to represent any expression.

Topic link icon Transact-SQL Syntax Conventions

Syntax

DATALENGTH ( expression ) 
Arguments

expression

Is an expression of any data type.

Return Types

bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise int.

Remarks

DATALENGTH is especially useful with varchar, varbinary, text, image, nvarchar, and ntext data types because these data types can store variable-length data.

The DATALENGTH of NULL is NULL.

ms173486.note(en-us,SQL.100).gifNote:
Compatibility levels can affect return values. For more information about compatibility levels, see ALTER DATABASE Compatibility Level (Transact-SQL).

Examples

The following example finds the length of the Name column in the Product table.

USE AdventureWorks;
GO
SELECT length = DATALENGTH(Name), Name
FROM Production.Product
ORDER BY Name;
GO
See Also

Reference

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

Help and Information

Getting SQL Server 2008 Assistance
Tags :


Page view tracker