Re: How to cast VARCHAR to BYTEA and vice-versa?

From: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
To: Lance Peterson <heart8reaker(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org, Michael(dot)Moran(at)igt(dot)com
Subject: Re: How to cast VARCHAR to BYTEA and vice-versa?
Date: 2005-03-15 09:09:04
Message-ID: 4236A630.E6952C1B@rodos.fzk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Seems my remembrance was wrong, ::TEXT::BYTEA causes
ERROR: cannot cast type text to bytea
on my 7.4.5 version too.
Looking into "Other Binary String Functions" within the manual,
sheds light on how to do it:

::TEXT::BYTEA
Function decode(string text, type text) Return Type bytea
select decode('123\\000456'::text, 'escape');
decode
------------
123\000456
(1 row)

::BYTEA::TEXT
Function encode(string bytea, type text) Return Type text
select encode('123\\000456'::bytea, 'escape');
encode
------------
123\000456
(1 row)

Sorry for the confusion I have caused,
especially because there already was a correct answer
sent by Ragnar.

Regards, Christoph

Lance Peterson wrote:
>
> Christoph, is that recent functionality? I'm running 7.4 and I get:
> ERROR: cannot cast type text to bytea
>
> I get similar errors when trying to go the other direction.
>
> On Mon, 14 Mar 2005 11:32:26 +0100, Christoph Haller <ch(at)rodos(dot)fzk(dot)de> wrote:
> > "Moran.Michael" wrote:
> > >
> > > Hello all,
> > >
> > > I have a table with a VARCHAR column that I need to convert to a BYTEA.
> > >
> > > How do I cast VARCHAR to BYTEA?
> > >
> > > The following doesn't seem to work as it yields the 'cannot cast varchar to
> > > bytea' error message:
> > >
> > > varchar_data::bytea
> > >
> > > On the same topic, how do I do the reverse, that is, how to I cast from
> > > BYTEA back to VARCHAR?
> > >
> > > Thanks,
> > > -Michael Moran
> > >
> > >
> > As far as I remember
> > varchar_data::TEXT::BYTEA
> > works. Vice versa dto.
> > Be aware BYTEA data may contain character codes
> > unconvertable to VARCHAR, especially '\0'.
> >
> > Regards, Christoph
> >

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tambet Matiisen 2005-03-15 09:33:33 Re: Parameterized views proposition
Previous Message Robert.Farrugia 2005-03-15 09:00:14 Re: Inserting values in arrays