LEN (Transact-SQL)
The LEN
function returns the number of characters of the specified string expression, excluding trailing blanks.
Syntax
LEN ( string_expression )
Arguments
Parameter | Type | Description |
---|---|---|
string_expression | nvarchar, varchar, nchar, char, binary, varbinary, or text | The string expression to be evaluated. If the expression evaluates to NULL, LEN returns NULL. |
Return Types
Returns int
. If string_expression
is NULL, the function returns NULL.
Remarks
- Trailing spaces are not counted.
- For
nvarchar
andnchar
, the length is measured in characters, not bytes. - When
string_expression
is of typetext
, the text value is first implicitly converted tovarchar(max)
before the length is calculated.
Examples
SELECT LEN('Hello') AS Length1; -- Returns 5
SELECT LEN('Hello ') AS Length2; -- Returns 5 (trailing blanks omitted)
SELECT LEN(NULL) AS Length3; -- Returns NULL
SELECT LEN(CAST(N'Unicode' AS nvarchar(20))) AS Length4; -- Returns 7