PostgreSQL LENGTH Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PostgreSQL LENGTH() function

LENGTH() function

The PostgreSQL length() function is used to find the length of a string i.e. number of characters in the given string.

Syntax :

length(<string>)

PostgreSQL Version : 9.3

Pictorial Presentation of PostgreSQL LENGTH() function

Example : PostgreSQL LENGTH() function :

In the example below the length function returns the length of the given string 'w3resource'.

  1. SELECT length('w3resource')  
  2. AS "Length of a String";  

Output :

 Length of a String
--------------------
                 10
(1 row)

Example of PostgreSQL LENGTH() function using column :

The example below, returns the first_name and the length of first_name ( how many characters contain in the first name ) from the employees where the length of first_name is more than 7.

  1. SELECT first_name,length(first_name)   
  2. AS "Length of a First Name"   
  3. FROM employees   
  4. WHERE length(first_name)>7;  

Output :

 first_name  | Length of a First Name
-------------+------------------------
 Alexander   |                      9
 Jose Manuel |                     11
 Alexander   |                      9
 Christopher |                     11
 Danielle    |                      8
 Harrison    |                      8
 Elizabeth   |                      9
 Jonathon    |                      8
 Kimberely   |                      9
 Jennifer    |                      8
 Jennifer    |                      8
(11 rows)