DATALENGTH

Transact-SQL Reference

Transact-SQL Reference

DATALENGTH

Returns the number of bytes used to represent any expression.

Syntax

DATALENGTH ( expression )

Arguments

expression

Is an expression of any type.

Return Types

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.

Note  Compatibility levels can affect return values. For more information about compatibility levels, see sp_dbcmptlevel.

Examples

This example finds the length of the pub_name column in the publishers table.

USE pubs
GO
SELECT length = DATALENGTH(pub_name), pub_name
FROM publishers
ORDER BY pub_name
GO

Here is the result set:

length      pub_name                                 
----------- ---------------------------------------- 
20          Algodata Infosystems                     
16          Binnet & Hardley                         
21          Five Lakes Publishing                    
5           GGG&G                                    
18          Lucerne Publishing                       
14          New Moon Books                           
17          Ramona Publishers                        
14          Scootney Books                           

(8 row(s) affected)

See Also

CAST and CONVERT

Data Types

System Functions