Contents
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
- PostgreSQL 8.3
- PostgreSQL 8.0
- now disregards trailing whitespace in
char
values (commit f27976c8)
- now disregards trailing whitespace in
- PostgreSQL 6.3
- added (commit 3551ee09)
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)
References
- PostgreSQL documentation: SQL String Functions and Operators
- PostgreSQL documentation: SQL Binary String Functions and Operators
- PostgreSQL documentation: Bit String Functions
- PostgreSQL documentation: Geometric Functions
- PostgreSQL documentation: Text Search Functions