length()

A function returning the number of elements in the provided parameter

length() is a system function returning the number of elements (characters, bytes etc.) in the provided parameter.

length() was added in PostgreSQL 6.3.

Usage

length ( text ) → integer
length ( bytea ) → integer
length ( bytes bytea, encoding name ) → integer
length ( bit ) → integer
length ( geometric_type ) → double precision
length ( tsvector ) → integer

Change history

Examples

Number of characters in a simple ASCII string:

postgres=# postgres=# SELECT length('ABC');
 length 
--------
      3
(1 row)

Number of characters in an UTF8 string containing multibyte characters:

postgres=# SELECT length('ほげほげ');
 length 
--------
      4
(1 row)

Number of bits in a bit string:

postgres=# SELECT length(B'10101');
 length 
--------
      5
(1 row)

Number of bytes in a binary string:

postgres=# SELECT length('\xdeadbeef'::bytea);
 length 
--------
      4
(1 row)

Sum of geometric path segments:

postgres=# SELECT length(path '((-1,0),(0,0),(1,4))');
      length       
-------------------
 9.595241580617241
(1 row)

Number of lexemes in a tsvector:

postgres=# SELECT length(to_tsvector('I am a cat'));
 length 
--------
      1
(1 row)

Categories

String manipulation, System function

See also

bit_length(), octet_length()