Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LEN() Function in SQL Server

  • Last Updated : 27 Nov, 2020

LEN() function calculates the number of characters of an input string, excluding the trailing spaces.

Syntax :

LEN(input_string)

Parameter –
This method accepts a single-parameter as mentioned above and described below :  

input_string –
It is an expression that can be a constant, variable, or column of either character or binary data.

Returns :

It returns the number of characters of an input string, excluding the trailing spaces.

Example-1: Return the length of a string

SELECT LEN('Geeksforgeeks');

Output :

13

Example-2: Check LEN() Function count trailing spaces or not

SELECT LEN('GFG       ') 
As Length_with_trailing_spaces;

Output :

Length_with_trailing_spaces
  3

Example-3: Check LEN() Function count leading spaces or not

SELECT LEN('       GFG') 
As Length_with_leading_spaces;

Output:

Length_with_leading_spaces
  10

Example-4: Using LEN() function with a column

Table — Player_Details

PlayerIdPlayerName>City
45Rohit SharmaMumbai
18Virat KohliBengaluru
7M S DhoniChennai
SELECT
    PlayerName,
    LEN(PlayerName) PlayerName_Length
FROM
    Player_Details

Output :

PlayerNamePlayerName_Length 
Rohit Sharma  12
Virat Kohli 11
MS Dhoni  8

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!