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
Syntax
length(string) | char_length(string) | octet_length(string)
Parameters
| Parameter | Description |
|---|---|
string |
The input text. |
Return value
An integer, or NULL.
Examples
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.
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
LENGTH()· SQLiteOCTET_LENGTH()· SQLite
Related
SUBSTRING()· Extract part of a string by 1-based position and length, or the part matching a regular expression.LPAD()· Pad a string on the left to a given length with spaces or another string. Longer strings are cut to that length.- SQL String Functions · tutorial