PostgreSQL 18 reference · String Functions

PostgreSQL LENGTH() Function

Return the number of characters in a string. char_length() is the standard name; octet_length() counts bytes.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

length(string)  |  char_length(string)  |  octet_length(string)

Parameters

ParameterDescription
string The input text.

Return value

An integer, or NULL.

Examples

Longest names
SELECT name, length(name) AS letters
FROM customers
ORDER BY letters DESC
LIMIT 3;

Output

        name        | letters
--------------------+---------
 Yukihiro Matsumoto |      18
 Margaret Hamilton  |      17
 Katherine Johnson  |      17
(3 rows)

Run this on your computer — see “Get Started” for setup.

Characters vs bytes
SELECT length('café') AS chars, octet_length('café') AS bytes;

Output

 chars | bytes
-------+-------
     4 |     5
(1 row)

Run this on your computer — see “Get Started” for setup.

In other languages

Was this page helpful?