SQL Server: DATALENGTH Function

DATALENGTH function 

This SQL Server tutorial explains how to use the DATALENGTH function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the DATALENGTH function returns the length of an expression, in bytes.

Syntax

The syntax for the DATALENGTH function in SQL Server (Transact-SQL) is:

DATALENGTH( expression )

Parameters or Arguments

expression

The data type that you wish to return the length for.

Note

  • The DATALENGTH functions counts both trailing spaces and preceeding spaces when calculating the length of the expression.
  • The DATALENGTH functions will return NULL, if the expression is NULL.
  • See also the LEN function which does not include trailing spaces in the length calculation.

Applies To

The DATALENGTH functions can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server DATALENGTH functions examples and explore how to use the DATALENGTH functions in SQL Server (Transact-SQL).

For example:

SELECT DATALENGTH('adglob.in');
Result: 8

SELECT DATALENGTH('    adglob.in    ');
Result:17

SELECT DATALENGTH(1234);
Result: 4

SELECT DATALENGTH('2004-05-01');
Result: 10

SELECT DATALENGTH(' ');
Result: 1

SELECT DATALENGTH('');
Result: 0

SELECT DATALENGTH(NULL);
Result: NULL

Leave a Reply