Skip to content

Latest commit

 

History

History
102 lines (82 loc) · 4.73 KB

len-transact-sql.md

File metadata and controls

102 lines (82 loc) · 4.73 KB
titledescriptionauthorms.authorms.datems.servicems.subservicems.topicf1_keywordshelpviewer_keywordsdev_langsmonikerRange
LEN (Transact-SQL)
LEN (Transact-SQL)
rwestMSFT
randolphwest
09/03/2015
sql
t-sql
reference
LEN
LEN_TSQL
LEN function
characters [SQL Server], number of
number of characters
TSQL
>= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || = azuresqldb-mi-current||=fabric

LEN (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Returns the number of characters of the specified string expression, excluding trailing spaces.

Note

To return the number of bytes used to represent an expression, use the DATALENGTH function.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

LEN ( string_expression ) 

Arguments

string_expression
Is the string expression to be evaluated. string_expression can be a constant, variable, or column of either character or binary data.

Return Types

bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int.

If you are using SC collations, the returned integer value counts UTF-16 surrogate pairs as a single character. For more information, see Collation and Unicode Support.

Remarks

LEN excludes trailing spaces. If that is a problem, consider using the DATALENGTH (Transact-SQL) function which does not trim the string. If processing a unicode string, DATALENGTH will return a number that may not be equal to the number of characters. The following example demonstrates LEN and DATALENGTH with a trailing space.

 DECLARE @v1 VARCHAR(40), @v2 NVARCHAR(40); SELECT @v1 ='Test of 22 characters ', @v2 ='Test of 22 characters '; SELECT LEN(@v1) AS [VARCHAR LEN] , DATALENGTH(@v1) AS [VARCHAR DATALENGTH]; SELECT LEN(@v2) AS [NVARCHAR LEN], DATALENGTH(@v2) AS [NVARCHAR DATALENGTH]; 

Note

Use the LEN to return the number of characters encoded into a given string expression, and DATALENGTH to return the size in bytes for a given string expression. These outputs may differ depending on the data type and type of encoding used in the column. For more information on storage differences between different encoding types, see Collation and Unicode Support.

Examples

The following example selects the number of characters and the data in FirstName for people located in Australia. This example uses the AdventureWorks database.

SELECT LEN(FirstName) AS Length, FirstName, LastName FROMSales.vIndividualCustomerWHERE CountryRegionName ='Australia'; GO 

Examples: [!INCLUDEssazuresynapse-md] and [!INCLUDEssPDW]

The following example returns the number of characters in the column FirstName and the first and last names of employees located in Australia.

USE AdventureWorks2022 GO SELECT DISTINCT LEN(FirstName) AS FNameLength, FirstName, LastName FROMdbo.DimEmployeeAS e INNER JOINdbo.DimGeographyAS g ONe.SalesTerritoryKey=g.SalesTerritoryKeyWHERE EnglishCountryRegionName ='Australia'; 

[!INCLUDEssResult]

FNameLength FirstName LastName ----------- --------- --------------- 4 Lynn Tsoflias 

See Also

DATALENGTH (Transact-SQL)
CHARINDEX (Transact-SQL)
PATINDEX (Transact-SQL)
LEFT (Transact-SQL)
RIGHT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)

close