convert()

A function for converting the encoding of a bytea string

convert() is a system function for converting a bytea string containing text between different encodings.

convert() was added in PostgreSQL 7.2.

Usage

convert ( bytes bytea, src_encoding name, dest_encoding name ) → bytea

An ERROR is raised if the provided encodings are not compatible or the string contains an invalid byte sequence for the source encoding.

convert_from() and convert_to() provide the same functionality without needing to specify the source or target string's encoding if that matches the current database's encoding.

Change history

Examples

Converting a single character between encodings:

postgres=# SELECT 'ä'::bytea, convert('ä'::bytea, 'UTF8', 'LATIN1');
 bytea  | convert 
--------+---------
 \xc3a4 | \xe4
(1 row)

Attempting to perform a conversion between incompatible encodings:

postgres=# SELECT convert('ほげほげ'::bytea, 'UTF-8', 'LATIN1');
ERROR:  character with byte sequence 0xe3 0x81 0xbb in encoding "UTF8" has no equivalent in encoding "LATIN1"

Attempting to perform a conversion where the provided string contains an invalid byte sequence for the source encoding:

postgres=# SELECT convert(E'\\xDEADBEEF', 'UTF8', 'LATIN1');
ERROR:  invalid byte sequence for encoding "UTF8": 0xbe

Categories

Locale and character set handling, String manipulation, System function

See also

convert_from(), convert_to()