length() 是一个系统函数,用于返回所提供参数中元素的数量(字符、字节等)。
length() 在 PostgreSQL 6.3 中添加。
用法
length (text) →integer
length (bytea) →integer
length (bytesbytea,encodingname) →integer
length (bit) →integer
length (geometric_type) →double precision
length (tsvector) →integer
变更历史
- PostgreSQL 8.3
- PostgreSQL 8.0
- 现在忽略
char值中的尾随空格(提交 f27976c8)
- 现在忽略
- PostgreSQL 6.3
- 添加(提交 3551ee09)
示例
简单 ASCII 字符串中的字符数
postgres=# postgres=# SELECT length('ABC');
length
--------
3
(1 row)
包含多字节字符的 UTF8 字符串中的字符数
postgres=# SELECT length('ほげほげ');
length
--------
4
(1 row)
位字符串中的位数
postgres=# SELECT length(B'10101');
length
--------
5
(1 row)
二进制字符串中的字节数
postgres=# SELECT length('\xdeadbeef'::bytea);
length
--------
4
(1 row)
几何路径段的总和
postgres=# SELECT length(path '((-1,0),(0,0),(1,4))');
length
-------------------
9.595241580617241
(1 row)
tsvector 中的词元数
postgres=# SELECT length(to_tsvector('I am a cat'));
length
--------
1
(1 row)
参考资料
- PostgreSQL 文档: SQL 字符串函数和运算符
- PostgreSQL 文档: SQL 二进制字符串函数和运算符
- PostgreSQL 文档: 位字符串函数
- PostgreSQL 文档: 几何函数
- PostgreSQL 文档: 文本搜索函数
