SQL Server: LEN Function

LEN functions

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

Description

In SQL Server (Transact-SQL), the LEN functions returns the length of the specified string. It is important to note that the LEN function does not include trailing space characters at the end the string when calculating the length.

Syntax

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

LEN( string )

Parameters or Arguments

string

The string to return the length for.

Note

  • The LEN function does NOT count trailing spaces at the end of the string when calculating the length of the string.
  • The LEN function does count spaces at the start of the string when calculating the length of the string.
  • And The LEN function will return NULL, if the string is NULL.
  • See also the DATALENGTH function which includes trailing spaces in the length calculation.

Applies To

The LEN function 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 LEN function examples and explore how to use the LEN function in SQL Server (Transact-SQL).

For example:

SELECT LEN('adglob.in');
Result: 9

SELECT LEN('adglob.in   ');
Result: 9         (trailing spaces are not included in the calculation)

SELECT LEN(' ');
Result: 0          (trailing spaces are not included in the calculation)

SELECT LEN('');
Result: 0

SELECT LEN(NULL);
Result: NULL

This Post Has One Comment

Leave a Reply