SQL LEN() Function

The LEN function is useful to count the number of characters in a column you specified as the parameter. LEN is short for length. The LEN function returns the number of characters including spaces or any character within that column.

You can use SQL Alias to name the result of the LEN function.

SQL LEN() Syntax

SELECT LEN(column_name) FROM TABLE

Consider the following table for this exercise.

Users

Firstname Lastname Salary DeptID
John Smith 1000 1
Mathew Simon 3000 1
Bill Steve 2200 1
Amanda Rogers 1800 2
Steve Hills 2800 2
Steve jobs 2400 2
bill cosby 700 3

Example # 1

SELECT LEN(Firstname) AS Firstname_Length FROM users

Result of the Query

Firstname_Length
5
5
5
6
5
4
5

You can also add other column if you wish.

Example # 2

SELECT Firstname, Lastname, LEN(Firstname) AS Firstname_Length FROM users
Firstname Lastname Firstname_Length
John Smith 5
Mathew Simon 5
Bill Steve 5
Amanda Rogers 6
Steve Hills 5
Steve jobs 4
bill cosby 5

Add new comment