Monday, April 24, 2017

LEN() in SQL Server

SQL LEN() function is used to calculate the length of the string. It returns the number of character in a string. LEN() works on numeric_column as well. NULL values are not considered.


            SELECT LEN(expression)

expression is the value of which length is to be calculated.

Return Types:

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

Example 1:
LEN() on numeric column.

SELECT * FROM TempEmployeePayHistory

SELECT Rate, LEN(Rate) FROM TempEmployeePayHistory

Example 2:
LEN() on column with NULL values.

          SELECT * FROM TempEmployeePayHistory

SELECT Rate, LEN(Rate) FROM TempEmployeePayHistory

Example 3:
LEN() on text column.

          SELECT * FROM DatabaseLog

          SELECT DISTINCT [Event], LEN([Event]) FROM DatabaseLog

Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.