Re: interpret bytea output as text / double encode()

From: Stefan Froehlich <postgresql(at)froehlich(dot)priv(dot)at>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: interpret bytea output as text / double encode()
Date: 2014-06-06 14:57:10
Message-ID: 20140606145710.GA4031@Debian-60-squeeze-64-minimal
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jun 06, 2014 at 10:30:56AM -0400, Tom Lane wrote:
> It's possible that what you are looking for is a binary-equivalent
> cast from text to bytea, which you could create like this:

> # create cast (text as bytea) without function;

Hm. No, actually it does not help. But playing around with the cast
revealed to me another thing. If I double-encode "12345", I have:

| # select encode('\x5c7833313332333333343335', 'escape');
| encode
| ---------------
| \\x3132333435
| (1 row)

Now the intermediate result contains a '\\' which I just ignored as
it is the usual quoting for a '\'. *But* of course this prevents the
string from being correctly interpreted as a bytea-sequence, so the
solution for the problem is:

| # select encode(right(encode('\x5c7833313332333333343335', 'escape'), -1)::bytea, 'escape');
| encode
| --------
| 12345
| (1 row)

Kind of a hack, but this works on tables as well and will save me a
lot of trouble. Thank you for the inspiration!

Stefan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message David G Johnston 2014-06-06 15:12:48 Re: How to select rows for which column has empty array ?
Previous Message Tom Lane 2014-06-06 14:39:43 Re: PostgreSQL 9.2.4 + CentOS 6.5 64 bit - segfault error in initdb