SQL Server LEN() Function
The LEN()
function in SQL Server determines the length of a string. It's a simple yet essential function for string manipulation and data validation.
LEN(): Definition and Usage
LEN()
counts the number of characters in a string. It's important to note that trailing spaces (spaces at the end of the string) are *not* included in the count, but leading spaces (spaces at the beginning) are.
Related Function
The DATALENGTH()
function returns the length of a string in bytes, which can be different from the character count if you're using multi-byte character sets.
Syntax
Syntax
LEN(string)
Parameter Values
Parameter | Description |
---|---|
string |
The string whose length you want to determine. If the input string is NULL , LEN() will return NULL . This is required. |
Examples
Finding the Length of a String
This example finds the length of the string 'W3Schools.com'.
Syntax
SELECT LEN('W3Schools.com');
Output
13
Finding the Length of a String with Leading and Trailing Spaces
This example shows that leading spaces are included but trailing spaces are not.
Syntax
SELECT LEN(' W3Schools.com ');
Output
14
Finding the Length of a Different String
Another example with a different string.
Syntax
SELECT LEN('2017-08');
Output
7