Function Reference

LENGTH

Returns the number of characters in a string.

PostgreSQL 17.9
LENGTH(string)
string
Source string
text

Return value

An integer, the number of characters, not bytes

Examples

String length

PostgreSQL 17.9
SELECT LENGTH('PostgreSQL');
length
10

Length of product names

PostgreSQL 17.9
SELECT good_name,
	LENGTH(good_name) AS name_length
FROM Goods;
good_namename_length
apartment fee13
phone fee9
bread5
milk4
red caviar10
cinema6
black caviar12
cough tablets13
potato6
pineapples10

Details

Unlike in MySQL, where LENGTH counts bytes, here it counts characters; the size in bytes is returned by OCTET_LENGTH. For the char(n) type, trailing spaces are not counted: LENGTH('ab '::char(5)) returns 2.

See also

Where to learn