Tuesday 29 October 2019

LEN () function in sql server


This function is used to get the length of the string. It returns the number of characters in a variable. It also removes the trailing spaces and then returns the length.
Syntax
LEN(expression)
See the example
declare @name varchar(50);
set @name='Bagesh';

select LEN(@name)
It will return 6
 

It excludes the trailing blank spaces while calculating the number of characters in the specified string.
See this example
 

Keep in mind: it excludes trailing black space only as we see above.
If the string is followed by blank spaces then it will be countable.


If we want to remove the blank spaces we need to use LTRIM() function after that we use the Len function to get the length of this string.
 

If the expression return NULL then LEN function will return NULL value.
 

If we want to return 0 if the expression return NULL then we need to use ISNULL function see below.


It will return the number of character only does not mean which data type we are using.
It does not support Text, NText and Image data type. See the below example.
 


No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts