=========================  二进制串函数和操作符 ========================= .. container:: sect1 :name: FUNCTIONS-BINARYSTRING .. container:: titlepage .. container:: .. container:: .. rubric:: 二进制串函数和操作符 :name: 二进制串函数和操作符 :class: title 本节描述那些检查和操作类型为\ ``bytea``\ 的值的函数和操作符。 SQL定义了一些使用关键字而不是逗号来分割参数的串函数。详情请见\ `表 `__\ 。PostgreSQL也提供了这些函数使用常规函数调用语法的版本(参阅\ `表 `__\ )。 .. note:: .. rubric:: 注意 :name: 注意 :class: title 本页中显示的示例结果假设服务器参数\ ``bytea_output`` \ 被设置为\ ``escape``\ (传统PostgreSQL格式)。 .. container:: table :name: FUNCTIONS-BINARYSTRING-SQL **表 SQL二进制串函数和操作符** .. container:: table-contents +-------------+-----------+-------------+-------------+-------------+ | 函数 | 返回类型 | 描述 | 例子 | 结果 | +=============+===========+=============+=============+=============+ | string | bytea | 串连接 | | \\Post\ | | ||\ | | | '\\Post'::\ | 'gres\000 | | \ \ | | | bytea || '\ | | | string | | | 047gres\\00\| | | | | | 0'::bytea | | +-------------+-----------+-------------+-------------+-------------+ | | int | 二进制串\ | | 5 | | | | 中的字节数 | octet_lengt\| | | octet_leng\ | | | h('jo\\000se| | | th(string) | | | '::bytea) | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 替换子串 | ove\ | T\\00\ | | overl\ | | | rlay('Th\\00| 2\\003mas | | ay(string | | | 0omas'::byt | | | placing \ | | | ea placing | | | \ | | | '\002\003': | | | string | | | :bytea from | | | from | | | 2 for 3) | | | int | | | | | | [for | | | | | | int ]) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | int | 指定\ | positi\ | 3 | | position( | | 子串的位置 | on('\000om' | | | substring | | | ::bytea in | | | in | | | 'Th\000omas | | | | | | '::bytea) | | | string) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 提取子串 | s\ | h\\000o | | substri\ | | | ubstring('T\| | | ng(string\ | | | h\000omas':\| | | [from | | | :bytea from\| | | int ] | | | \ 2 for 3) | | | [for | | | | | | \ int ]) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 从 | \ trim('\ | Tom | | trim( | | \ string | 000\001'::b\| | | [both] | | \ 的开头 | ytea from ' | | | \ bytes | | 或结尾删除 | \000Tom\001 | | | from | | 只包含出现 | '::bytea) | | | \ | | 在\ byt\ | | | | string) | | es \ 中字\| | | | | | 节的最长串 | | | +-------------+-----------+-------------+-------------+-------------+ 还有一些二进制串处理函数可以使用,在\ `表 `__\ 列出。 其中有一些是在内部使用,用于实现\ `表 `__\ 列出的 SQL 标准串函数。 .. container:: table :name: FUNCTIONS-BINARYSTRING-OTHER **表 其他二进制串函数** .. container:: table-contents +-------------+-----------+-------------+-------------+-------------+ | 函数 | 返回类型 | 描述 | 例子 | 结果 | +=============+===========+=============+=============+=============+ | | bytea | 从 | btrim | trim | | btr\ | | \ string | ('\\000trim\| | | im(string | | \ 的开头 | 001'::bytea | | | bytea , | | 或结尾删除 | , '\000\001 | | | bytes | | 只由出现在\ | '::bytea) | | | bytea ) | | bytes | | | | | | \ 中字节组 | | | | | | 成的最长串 | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 从\ st | | 1\ | | deco\ | | ring \ 中 | decode('12 | 23\\000456 | | de(string | | 的文本表示 | 3\\000456', | | | text , | | 解码二进制 | 'escape') | | | | | 数据。\ | | | | format | | format \ | | | | text ) | | 的参数和在 | | | | | | \ encode | | | | | | \ 中一样。 | | | +-------------+-----------+-------------+-------------+-------------+ | | text | 将二进制 | encode | 1\ | | en\ | | 数据编码为 | ('123\\00045| 23\\000456 | | code(data | | 一个文本表 | 6'::bytea, | | | bytea , | | 示。支持的 | 'escape') | | | | | 格式有:\ | | | | format | | base64 \ | | | | text ) | | 、\ hex | | | | | | \ 、\ esc | | | | | | ape \ 。\ | | | | | | escape \ | | | | | | 将零字节和 | | | | | | 高位组字节 | | | | | | 转换为八进 | | | | | | 制序列(\ | | | | | | \ \ n | | | | | | nn \ )和 | | | | | | 双反斜线。 | | | +-------------+-----------+-------------+-------------+-------------+ | | int | 从\ | g\ | 1 | | get_bit | | 串中抽取位 | et_bit('Th\\| | | (string , | | | 000omas'::b\| | | \ | | | ytea, 45) | | | offset) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | int | 从串\ | g\ | 109 | | get_byte | | 中抽取字节 | et_byte('Th | | | (string, | | | \\000omas'::| | | \ | | | bytea, 4) | | | offset) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | int | 二进\ | lengt\ | 5 | | length | | 制串的长度 | h('jo\000se | | | (string) | | | '::bytea) | | +-------------+-----------+-------------+-------------+-------------+ | | text | 计\ | md5(\ | 8\ | | md5 | | 算\ stri | 'Th\\000omas| ab2d3c9689a | | (string) | | ng \ 的MD | '::bytea) | af18b4958c3 | | | | 5哈希码,以 | | 34c82d8b1 | | | | 十六进制形 | | | | | | 式返回结果 | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 设\ | set_b\ | T\ | | set_bit | | 置串中的位 | it('Th\\000 | h\\000omAs | | (string, | | | omas'::bytea| | | \ | | | , 45, 0) | | | offset , | | | | | | \ | | | | | | newvalue ) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | 设置\ | set_b\ | T\ | | set_byte | | 串中的字节 | yte('Th\\000| h\\000o@as | | (string, | | | omas'::bytea| | | \ | | | , 4, 64) | | | offset , | | | | | | \ | | | | | | newvalue) | | | | | +-------------+-----------+-------------+-------------+-------------+ | | bytea | SHA-224哈希 | sha224 | \\x230\ | | sha224\ | | | ('abc') | 97d223405d8 | | (bytea) | | | | 228642a477b | | | | | | da255b32aad | | | | | | bce4bda0b3f | | | | | | 7e36c9da7 | +-------------+-----------+-------------+-------------+-------------+ | | bytea | SHA-256哈希 | sha256 | \\x\ | | sha256\ | | | ('abc') | ba7816bf8f0 | | (bytea) | | | | 1cfea414140 | | | | | | de5dae2223b | | | | | | 00361a39617 | | | | | | 7a9cb410ff6 | | | | | | 1f20015ad | +-------------+-----------+-------------+-------------+-------------+ | | bytea | SHA-384哈希 | sha384 | \\x\ | | sha384\ | | | ('abc') | cb00753f45 | | (bytea) | | | | a35e8bb5a03 | | | | | | d699ac65007 | | | | | | 272c32ab0ed | | | | | | ed1631a8b60 | | | | | | 5a43ff5bed8 | | | | | | 086072ba1e7 | | | | | | cc2358baeca | | | | | | 134c825a7 | +-------------+-----------+-------------+-------------+-------------+ | | bytea | SHA-512哈希 | sha512 | \\x\ | | sha512\ | | | ('abc') | ddaf35a19 | | (bytea) | | | | 3617abacc41 | | | | | | 7349ae20413 | | | | | | 112e6fa4e89 | | | | | | a97ea20a9ee | | | | | | ee64b55d39a | | | | | | 2192992a274 | | | | | | fc1a836ba3c | | | | | | 23a3feebbd4 | | | | | | 54d4423643c | | | | | | e80e2a9ac94 | | | | | | fa54ca49f | +-------------+-----------+-------------+-------------+-------------+ ``get_byte``\ 和\ ``set_byte``\ 把一个二进制串中的一个字节计数为字节 0。\ ``get_bit``\ 和\ ``set_bit``\ 在每一个字节中从右边起计数位;例如位 0 是第一个字节的最低有效位,而位 15 是第二个字节的最高有效位。 注意由于历史原因,函数\ ``md5``\ 返回的是一个十六进制编码的\ ``text``\ 值,而SHA-2函数返回类型\ ``bytea``\ 。可以使用函数\ ``encode``\ 和\ ``decode``\ 在两者之间转换,例如\ ``encode(sha256('abc'), 'hex')``\ 可以得到一个十六进制编码的文本表示。 参见\ `节 `__\ 中的聚集函数\ ``string_agg``\ 以及\ `节 `__\ 中的大对象函数。