PostgreSQL CHAR LENGTH Function

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

PostgreSQL CHAR_LENGTH() function

CHAR_LENGTH() function

The PostgreSQL char_length function or character_length function is used to count the number of characters in a specified string. The CHARACTER_LENGTH() function is similar to CHAR_LENGTH() function.

Syntax :

char_length (string)

Parameters :

Name Description
string A string whose length is to be retrieved.

Example : PostgreSQL CHAR_LENGTH() function

SELECT char_length('w3resource') AS "Length of a String";

Output :

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

PostgreSQL CHAR_LENGTH() function using Column :

If we want to display the first name, last name and concatenate of first name and last name and number of character after concatenation of those employee who belong to the dept 20 from the employee table, the following SQL can be executed:

SELECT first_name,last_name,
first_name||' '||last_name AS "Name of Employee",
char_length(first_name||' '||last_name ) AS "Length of Name"
FROM employees
WHERE department_id=20;

Output :

 first_name | last_name | Name of Employee  | Length of Name
------------+-----------+-------------------+----------------
 Michael    | Hartstein | Michael Hartstein |             17
 Pat        | Fay       | Pat Fay           |              7
(2 rows)