Re: varchar::bytea fails when varchar contains backslash

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Matthias Apitz" <guru(at)unixarea(dot)de>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: varchar::bytea fails when varchar contains backslash
Date: 2022-02-21 10:59:55
Message-ID: 87c8627f-677a-45f7-b577-533df086377c@manitou-mail.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Matthias Apitz wrote:

> I wanted to look into a column of type varchar converting the content
> with ::bytea to a hex string, but this fails when the column contains a
> backslash:

Yes, casting from text to bytea cannot be used for that.
The convert_to() function must be used instead.

From the doc at
https://www.postgresql.org/docs/current/functions-binarystring.html

convert_to ( string text, dest_encoding name ) → bytea

Converts a text string (in the database encoding) to a binary string encoded
in encoding dest_encoding (see Section 24.3.4 for available conversions).

convert_to('some_text', 'UTF8') → \x736f6d655f74657874

Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: https://www.manitou-mail.org
Twitter: @DanielVerite

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Zheng Li 2022-02-21 15:53:43 Support logical replication of DDLs
Previous Message Tomas Pospisek 2022-02-21 10:20:01 Re: Detecting schema changes via WAL logs