当前 PostgreSQL 版本不在支持生命周期内。
您可以参阅
当前版本的对应页面,或其他在上面列出的活跃大版本。
本节描述用于检查和操作 bytea 类型值的函数和操作符。
SQL定义了一些使用关键字而不是逗号来分割参数的串函数。详情请见Table 9.12。PostgreSQL也提供了这些函数使用常规函数调用语法的版本(参阅Table 9.13)。
Note
本页显示的示例结果假定服务器参数 bytea_output 设为 escape(传统的 PostgreSQL 格式)。
Table 9.12. SQL二进制串函数和操作符
| 函数 |
返回类型 |
描述 |
示例 |
结果 |
string || string |
bytea |
字符串串接 |
'\\Post'::bytea || '\047gres\000'::bytea |
\\Post'gres\000 |
octet_length(string) |
int |
二进制串中的字节数 |
octet_length('jo\000se'::bytea) |
5 |
overlay(string placing string from int [for int]) |
bytea |
替换子字符串 |
overlay('Th\000omas'::bytea placing '\002\003'::bytea from 2 for 3) |
T\\002\\003mas |
position(substring in string) |
int |
指定子字符串的位置 |
position('\000om'::bytea in 'Th\000omas'::bytea) |
3 |
substring(string [from int] [for int]) |
bytea |
提取子字符串 |
substring('Th\000omas'::bytea from 2 for 3) |
h\000o |
trim([both] bytes from string) |
bytea |
从 string 的开头和结尾移除仅由 bytes 中字节组成的最长字符串 |
trim('\000\001'::bytea from '\000Tom\001'::bytea) |
Tom |
还有一些二进制串处理函数可以使用,在Table 9.13列出。 其中有一些是在内部使用,用于实现Table 9.12列出的 SQL 标准串函数。
Table 9.13. 其他二进制串函数
| 函数 |
返回类型 |
描述 |
示例 |
结果 |
btrim(string bytea, bytes bytea) |
bytea |
从 string 的开头和结尾移除仅由 bytes 中字节组成的最长字符串 |
btrim('\000trim\001'::bytea, '\000\001'::bytea) |
trim |
decode(string text, format text) |
bytea |
从 string 中的文本表示解码二进制数据。format 的选项与 encode 相同。 |
decode('123\000456', 'escape') |
123\000456 |
encode(data bytea, format text) |
text |
将二进制数据编码为文本表示。支持的格式为:base64、hex、escape。escape 将零字节和最高位为 1 的字节转换为八进制序列(\nnn),并将反斜杠加倍。 |
encode('123\000456'::bytea, 'escape') |
123\000456 |
get_bit(string, offset) |
int |
从字符串中提取位 |
get_bit('Th\000omas'::bytea, 45) |
1 |
get_byte(string, offset) |
int |
从字符串中提取字节 |
get_byte('Th\000omas'::bytea, 4) |
109 |
length(string) |
int |
二进制串的长度 |
length('jo\000se'::bytea) |
5 |
md5(string) |
text |
计算 string 的 MD5 哈希,并以十六进制返回结果 |
md5('Th\000omas'::bytea) |
8ab2d3c9689aaf18b4958c334c82d8b1 |
set_bit(string, offset, newvalue) |
bytea |
设置字符串中的位 |
set_bit('Th\000omas'::bytea, 45, 0) |
Th\000omAs |
set_byte(string, offset, newvalue) |
bytea |
设置字符串中的字节 |
set_byte('Th\000omas'::bytea, 4, 64) |
Th\000o@as |
sha224(bytea) |
bytea |
SHA-224 哈希 |
sha224('abc') |
\x23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7 |
sha256(bytea) |
bytea |
SHA-256 哈希 |
sha256('abc') |
\xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
sha384(bytea) |
bytea |
SHA-384 哈希 |
sha384('abc') |
\xcb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7 |
sha512(bytea) |
bytea |
SHA-512 哈希 |
sha512('abc') |
\xddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f |
函数get_byte和set_byte把二进制串中的第一个字节编号为字节 0。 函数get_bit和set_bit在每一个字节中从右边起计数位; 例如位 0 是第一个字节的最低有效位,而位 15 是第二个字节的最高有效位。
注意,由于历史原因,md5 函数返回十六进制编码的 text 值,而 SHA-2 函数返回 bytea。可以使用 encode 和 decode 在两者之间转换,例如使用 encode(sha256('abc'), 'hex') 得到十六进制编码的文本表示。
参见Section 9.20中的聚合函数string_agg以及Section 34.4中的大对象函数。