octet_length() 是一个系统函数,它返回给定参数中的字节数(八位字节)。
octet_length() 在 PostgreSQL 6.4 中添加。
用法
octet_length ( text ) → integer
octet_length ( bytea ) → integer
octet_length ( bit ) → integer
变更历史
- PostgreSQL 7.0
- 尾部空格现在被计为字符(提交 cfe71771)
- PostgreSQL 6.4
- 添加(提交 f554af0a)
示例
使用 octet_length() 获取简单 ASCII 字符串中的字节数
postgres=# SELECT octet_length('ABC');
octet_length
--------------
3
(1 row)
使用 octet_length() 获取包含多字节字符的 UTF8 字符串中的字节数
postgres=# SELECT octet_length('ほげほげ');
octet_length
--------------
12
(1 row)
使用 octet_length() 获取二进制字符串中的字节数
postgres=# SELECT octet_length('\xdeadbeef'::bytea);
octet_length
--------------
4
(1 row)
参考资料
- PostgreSQL 文档: SQL 字符串函数和运算符
- PostgreSQL 文档: SQL 二进制字符串函数和运算符
- PostgreSQL 文档: 位字符串函数
