本节描述用于检查和操作 bytea 类型值的函数和操作符。
SQL定义了一些具有特殊语法的二进制串函数,其中使用某些关键字而不是逗号来分隔参数。详情见表 9.9。其中一些函数也使用常规的函数调用语法实现(见表 9.10)。
表 9.9. SQL二进制串函数和操作符
| 函数 |
返回类型 |
描述 |
示例 |
结果 |
string || string |
bytea |
字符串串接 |
'\\\\Post'::bytea || '\\047gres\\000'::bytea |
\\Post'gres\000 |
octet_length(string) |
integer |
二进制串中的字节数 |
octet_length('jo\\000se'::bytea) |
5 |
position(substring in string) |
integer |
指定子串的位置 |
position('\\000om'::bytea in 'Th\\000omas'::bytea) |
3 |
substring(string [from integer] [for integer]) |
bytea |
提取子串 |
substring('Th\\000omas'::bytea from 2 for 3) |
h\000o |
trim([both] bytes from string) |
bytea |
移除string开始端和结束端仅含bytes中的字节(默认为空格)组成的最长字符串 |
trim('\\000'::bytea from '\\000Tom\\000'::bytea) |
Tom |
get_byte(string, offset) |
integer |
从字符串中抽取字节。 |
get_byte('Th\\000omas'::bytea, 4) |
109 |
set_byte(string, offset, newvalue) |
bytea |
在字符串中设置字节。 |
set_byte('Th\\000omas'::bytea, 4, 64) |
Th\000o@as |
get_bit(string, offset) |
integer |
从字符串中抽取位。 |
get_bit('Th\\000omas'::bytea, 45) |
1 |
set_bit(string, offset, newvalue) |
bytea |
在字符串中设置位。 |
set_bit('Th\\000omas'::bytea, 45, 0) |
Th\000omAs |
还有一些其他的二进制串处理函数可用, 列于表 9.10。其中一些在内部用于实现 表 9.9中列出的 SQL 标准字符串函数。
表 9.10. 其他二进制串函数
| 函数 |
返回类型 |
描述 |
示例 |
结果 |
btrim(string bytea bytes bytea) |
bytea |
移除string开始端和结束端仅由bytes中的字节组成的最长字符串。 |
btrim('\\000trim\\000'::bytea, '\\000'::bytea) |
trim |
length(string) |
integer |
二进制串的长度 |
length('jo\\000se'::bytea) |
5 |
decode(string text, type text) |
bytea |
解码string中先前用encode编码的二进制串。参数类型与encode中的相同。 |
decode('123\\000456', 'escape') |
123\000456 |
encode(string bytea, type text) |
text |
把二进制串编码为仅含ASCII的表示。支持的 类型有:base64、hex、escape。 |
encode('123\\000456'::bytea, 'escape') |
123\000456 |