MS SQL Server LEN() Function

Sometime you may need to determine the length of your desired text in MS SQL Server. There is a function called LEN in MS SQL Server by which you can do this. LEN function returns the number of characters of the specified string expression.

Let’s see the function structure:

LEN(string_expression)

In this function there is only one parameter. Parameter string_expression can be a constant, variable, or column of either character or binary data. LEN function returns bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int..

The following example uses LEN to determine length of the variable.

DECLARE @input varchar(60);
SET @input = 'Minhajur Rahman Khan   '; SELECT LEN(@input) AS 'before_rtrim', LEN(RTRIM(@input)) AS 'after_rtrim';

Here is the result set.

before_rtrim after_rtrim
23 20

I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.

In this tutorial, I have shown you how to to determine the length of your desired text in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.

Add a Comment