LEN (Transact-SQL)
Returns the number of characters of the specified string expression, excluding trailing blanks.
Syntax
LEN ( string_expression )
Parameters
| Parameter | Description |
|---|---|
string_expression |
Is an expression of any character data type. The expression can be a column, variable, literal, or a return value of an expression. If the expression is NULL, the return value is NULL. |
Return Type
Returns int. The return type is int regardless of the argument type.
Remarks
- Trailing blanks are not counted. To count trailing blanks, use DATALENGTH.
- If the
string_expressionis Unicode, each character counts as one, independent of its byte size. - For
NVARCHAR(max),VARCHAR(max), orVARBINARY(max), the maximum length returned is 2,147,483,647.
Examples
SELECT LEN('Hello World') AS Length; -- Returns 11
SELECT LEN('Hello ') AS Length; -- Returns 5 (trailing blanks ignored)
SELECT LEN(NULL) AS Length; -- Returns NULL