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
PlayerId | PlayerName | >City |
---|---|---|
45 | Rohit Sharma | Mumbai |
18 | Virat Kohli | Bengaluru |
7 | M S Dhoni | Chennai |
SELECT PlayerName, LEN(PlayerName) PlayerName_Length FROM Player_Details
Output :
PlayerName | PlayerName_Length |
---|---|
Rohit Sharma | 12 |
Virat Kohli | 11 |
MS Dhoni | 8 |