LENGTH Function - SQL


Overview


The LENGTH function in SQL returns the number of characters in a given string. It is useful for determining the length of a string.

Example:

SELECT LENGTH('Hello')

Syntax:

SELECT LENGTH(string_expression)

string_expression is the string for which you want to determine the number of characters.

Sample Data:

department_id first_name
3 Frank
2 Jane
3 Ashley
NULL Glenn
2 Kelly
1 Richard
1 George
5 Kyle
2 James
1 Gustavo

Example: Hard-coded value


In this example, we are returning the number of characters in the string ‘Hello’ and the final output is 5.


Example: Query without the WHERE Statement


In this example, we are returning the number of characters that is in each value of the first_name column. The first name is ‘Frank’ and you can see that the first output is 5.


Example: Query with the WHERE Statement


In this example, we are returning the number of characters that is in each value of the first_name column where the department_id equals 1. The first name on the list with a department_id of 1 is ‘Richard’ and the first output on the list equals 7.