9.5. 二进制字符串函数和运算符
本节介绍了用于检查和处理bytea
类型的值的函数和运算符。
SQL 定义了一些使用关键字而不是逗号分隔参数的字符串函数。详细信息在Table 9.11中。 PostgreSQL 还提供了使用常规函数调用语法的这些函数的版本(请参见Table 9.12)。
表 9.11. SQL 二进制字符串函数和运算符
Function | Return Type | Description | Example | Result |
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 | Replace substring | 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 | Extract substring | 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.12中列出。其中一些用于内部以实现Table 9.11中列出的 SQL 标准字符串函数。
表 9.12. 其他二进制字符串函数
Function | Return Type | Description | Example | Result |
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 将零字节和高位设置字节转换为八进制序列(\ * 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) | 8ab2d3c9689aaf18 b4958c334c82d8b1 |
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 |
get_byte
和set_byte
将二进制字符串的第一个字节编号为字节 0.get_bit
和set_bit
在每个字节中从右数位;例如,位 0 是第一个字节的最低有效位,而位 15 是第二个字节的最高有效位。
另请参见Section 9.20中的聚合函数string_agg
和Section 34.4中的大对象函数。