DATALENGTH (TSQL Function)

Returns the length of a string.

Trailing blanks are not discarded.

Syntax

DATALENGTH ( string_expression )

Arguments

string_expression
is a string expression of type varchar.

Return Types

Returns int.

Remarks

This function returns the full length of the string.

The similar function LEN excludes trailing blanks.

This function is exactly the same as the RSQL function CHARLEN.

In MS SQL Server, DATALENGTH returns the number of bytes of the string, not the number of characters !
In most cases, you should use CHARLEN instead.

Examples

PRINT LEN('guépard');
PRINT DATALENGTH('guépard');
PRINT LEN('guépard      ');        -- trailing blanks are excluded
PRINT DATALENGTH('guépard      '); -- trailing blanks are not excluded
PRINT LEN('');
PRINT DATALENGTH('');

The result is:

7
7
7    <--- LEN() excludes trailing blanks
13
0
0