chr() 是一个系统函数,用于将整数转换为 ASCII 或 UTF8 字符。
chr() 添加于 PostgreSQL 7.1。
用法
chr (integer) →text
使用 UTF8 编码时,整数参数被视为 Unicode 代码点。在其他编码中,参数必须表示一个 ASCII 字符。
不允许使用 0,因为它无法由文本数据类型存储。
如果提供无效的输入参数值,则会引发错误。
要获取字符的数值代码,请使用 ascii()。
变更历史
- PostgreSQL 9.4
- 现在只接受符合 RFC 3629 的有效 UTF8 字符的值 (提交 7894ac50)
- PostgreSQL 7.1
示例
chr() 的基本用法示例
postgres=# SELECT chr(97); chr ----- a (1 row)
使用其 UTF8 代码点返回 Unicode 字符
postgres=# SELECT chr(12354); chr ----- あ (1 row)
在非 UTF8 数据库中,只能提供有效的 ASCII 值
locale_test=# \l locale_test
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------+----------+----------+----------------+----------------+-------------------
locale_test | postgres | LATIN1 | de_DE.iso88591 | de_DE.iso88591 |
(1 row)
locale_test=# SELECT chr(12354);
ERROR: requested character too large for encoding: 12354
当客户端编码不是 UTF8 时,也适用相同情况
postgres=# SET client_encoding TO latin1; SET postgres=# SELECT chr(12354); ERROR: character with byte sequence 0xe3 0x81 0x82 in encoding "UTF8" has no equivalent in encoding "LATIN1"
无法提供 0
postgres=# SELECT chr(0); ERROR: null character not permitted
参考资料
- PostgreSQL 文档: 其他字符串函数
