SQL Server DATALENGTH() Function
THE WORLD'S LARGEST WEB DEVELOPER SITE

SQL Server DATALENGTH() Function

❮ SQL Server Functions

Example

Return the length of an expression (in bytes):

SELECT DATALENGTH('W3Schools.com');
Try it Yourself »

Definition and Usage

The DATALENGTH() function returns the length of an expression (in bytes).

Note: The DATALENGTH() function counts both leading and trailing spaces when calculating the length of the expression.

Tip: Also see the LEN() function.

Syntax

DATALENGTH(expression)

Parameter Values

Parameter Description
expression Required. The data type you wish to return the length for. If expression is NULL, DATALENGTH() will return NULL

Technical Details

Works in: SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

More Examples

Example

Return the length of an expression (in bytes) (counts both leading and trailing spaces):

SELECT DATALENGTH('   W3Schools.com   ');
Try it Yourself »

Example

Return the length of an expression (in bytes):

SELECT DATALENGTH('2017-08');
Try it Yourself »

❮ SQL Server Functions