cytrinox
cytrinox

Reputation: 1946

Convert an UTF-8 varchar to bytea with latin1 encoding in PostgreSQL

I'm porting a proprietary (and old) checksum algorithm from a MySQL procedure to PostgreSQL 8.4.

The whole database is UTF-8, but for this algorithm I need to convert the UTF-8 input to a bytea value with latin1 encoding. In MySQL, variables can be of different encoding and conversion is performed on-the-fly. Is there any function in PostgreSQL to do such an conversion?

The only alternative I see is to write a custom utf8_convert() C function which returns a bytea value and uses internally iconv() to convert the input to latin1. But I want to avoid such C functions.

Upvotes: 0

Views: 5770

Answers (1)

Tometzky
Tometzky

Reputation: 23920

From String Functions and Operators:

convert_to(text_in_database,'LATIN1')

But you have to be sure that the text can be encoded in Latin1 — you'll get an exception otherwise.

Upvotes: 2

Related Questions