chr()

A function for converting an integer into a character

chr() is a system function converting an integer into an ASCII or UTF8 character.

chr() was added in PostgreSQL 7.1.

Usage

chr ( integer ) → text

When using the UTF8 encoding, the integer parameter is considered as a Unicode code point. In other encodings the parameter must represent an ASCII character.

0 is not permitted as it cannot be stored by text data types.

An error is raised if an invalid input parameter value is provided.

Change history

Examples

Basic usage example for chr():

postgres=# SELECT chr(97);
 chr 
-----
 a
(1 row)

Returning a Unicode character using its UTF8 code point:

postgres=# SELECT chr(12354);
 chr 
-----
 あ
(1 row)

In a non-UTF8 database, only valid ASCII values can be provided:

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

The same applies when the client encoding is not 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 cannot be provided:

postgres=# SELECT chr(0);
ERROR:  null character not permitted

Categories

String manipulation, System function

See also

ascii()