SQL Tutorial SQL Advanced SQL Database SQL References

SQL Server LEN() Function



The SQL Server (Transact-SQL) LEN() function returns the number of characters of the specified string expression, excluding trailing spaces.

Note: To return the number of bytes used to represent the string expression, DATALENGTH() function can be used. It includes trailing spaces in the length calculation.

Syntax

LEN(string)

Parameters

string Required. Specify the string to return the length for.

Return Value

Returns the number of characters of the specified string.

Example 1:

The example below shows the usage of LEN() function.

SELECT LEN('12345');
Result: 5

SELECT LEN('ABCDE');
Result: 5

SELECT LEN(12345);
Result: 5

SELECT LEN('AlphaCodingSkills');
Result: 17

SELECT LEN('Alpha Coding Skills');
Result: 19

SELECT LEN(NULL);
Result: NULL

SELECT LEN('');
Result: 0

SELECT LEN(' ');
Result: 0

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

The statement given below can be used to get the length of records of City column.

SELECT *, LEN(City) AS LEN_Value FROM Employee;

The query will produce the following result:

EmpIDNameCityAgeLEN_Value
1JohnLondon256
2MarryNew York248
3JoParis275
4KimAmsterdam309
5RameshNew Delhi289
6HuangBeijing287

❮ SQL Server Functions