二进制字串函数和操作符

From PostgreSQL 中文维基, PostgreSQL 中文站, PostgreSQL 中国社区, PostgreSQL Chinese community

Jump to: navigation, search

[编辑] 二进制字串函数和操作符

本节描述那些检查和操作类型为 bytea 的数值的函数和操作符。

SQL 定义了一些有特殊语法的字串函数,在这些函数里使用特殊关键字而不是逗号来分隔参数。详情请见表 9-8。一些函数也实现了使用常用语法进行函数调用的方法。(参阅表 9-9。)

表 9-8. SQL 二进制字串函数和操作符
函数 返回类型 描述 例子 结果
string || string bytea 字串连接 '\\\\Post'::bytea || '\\047gres\\000'::bytea \\Post'gres\000
octet_length(string) int 二进制字串中的字节数目 octet_length('jo\\000se'::bytea) 5
pg_column_size(string) int 存储该数值需要的字节数,这个数值可能是压缩过的 pg_column_size('jo\\000se'::bytea) 5
position(substring in string) int 指定子字串的位置 position('\\000om'::bytea in 'Th\\000omas'::bytea) 3
substring(string [from integer] [for int]) 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) int 从字串中抽取字节。 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) int 从字串中抽取位。 get_bit('Th\\000omas'::bytea, 45) 1
set_bit(string, offset, newvalue) bytea 设置字串中的位。 set_bit('Th\\000omas'::bytea, 45, 0) Th\000omAs

还有一些二进制字串处理函数可以使用,在表 9-9列出。其中有一些是在内部使用,用于实现表 9-8列出的 SQL 标准的字串函数的。

表 9-9. 其它二进制字串函数
函数 返回类型 描述 例子 结果
btrim(string bytea, bytes bytea) bytea 从 string 的开头和结尾删除(截断) 只包含在 bytes 里面的字节的最长的字串。 btrim('\\000trim\\000'::bytea,'\\000'::bytea) trim
length(string) int 二进制字串的长度 length('jo\\000se'::bytea) 5
md5(string) text 计算 string 的 MD5 散列值,以十六进制方式返回结果。 md5('Th\\000omas'::bytea) 8ab2d3c9689aaf18 b4958c334c82d8b1
decode(string text, type text) bytea 把前面用encode()编码的放在 string 里的 二进制字串解码。参数类型和encode()里的一样。 decode('123\\000456', 'escape') 123\000456
encode(string bytea, type text) text 把二进制字串编码为只包含 ASCII 的表现形式。 类型是∶ base64,hex,escape。 encode('123\\000456'::bytea, 'escape') 123\000456
Personal tools