pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to cast VARCHAR to BYTEA and vice-versa?
5+ messages / 4 participants
[nested] [flat]
* How to cast VARCHAR to BYTEA and vice-versa?
@ 2005-03-08 15:31 Moran.Michael <Michael.Moran@IGT.com>
0 siblings, 2 replies; 5+ messages in thread
From: Moran.Michael @ 2005-03-08 15:31 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to cast VARCHAR to BYTEA and vice-versa?
@ 2005-03-14 08:46 Ragnar Hafstað <gnari@simnet.is>
parent: Moran.Michael <Michael.Moran@IGT.com>
1 sibling, 0 replies; 5+ messages in thread
From: Ragnar Hafstað @ 2005-03-14 08:46 UTC (permalink / raw)
To: Moran.Michael <Michael.Moran@IGT.com>; +Cc: pgsql-sql
On Tue, 2005-03-08 at 07:31 -0800, 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?
have you looked at the encode() and decode() functions ?
http://www.postgresql.org/docs/7.4/interactive/functions-string.html
gnari
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to cast VARCHAR to BYTEA and vice-versa?
@ 2005-03-14 10:32 Christoph Haller <ch@rodos.fzk.de>
parent: Moran.Michael <Michael.Moran@IGT.com>
1 sibling, 1 reply; 5+ messages in thread
From: Christoph Haller @ 2005-03-14 10:32 UTC (permalink / raw)
To: Moran.Michael <Michael.Moran@IGT.com>; +Cc: pgsql-sql
"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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to cast VARCHAR to BYTEA and vice-versa?
@ 2005-03-14 17:31 Lance Peterson <heart8reaker@gmail.com>
parent: Christoph Haller <ch@rodos.fzk.de>
0 siblings, 1 reply; 5+ messages in thread
From: Lance Peterson @ 2005-03-14 17:31 UTC (permalink / raw)
To: pgsql-sql
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@rodos.fzk.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
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/docs/faq
>
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: How to cast VARCHAR to BYTEA and vice-versa?
@ 2005-03-15 09:09 Christoph Haller <ch@rodos.fzk.de>
parent: Lance Peterson <heart8reaker@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Christoph Haller @ 2005-03-15 09:09 UTC (permalink / raw)
To: Lance Peterson <heart8reaker@gmail.com>; +Cc: pgsql-sql; Michael.Moran@igt.com
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@rodos.fzk.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
> >
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2005-03-15 09:09 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-03-08 15:31 How to cast VARCHAR to BYTEA and vice-versa? Moran.Michael <Michael.Moran@IGT.com>
2005-03-14 08:46 ` Ragnar Hafstað <gnari@simnet.is>
2005-03-14 10:32 ` Christoph Haller <ch@rodos.fzk.de>
2005-03-14 17:31 ` Lance Peterson <heart8reaker@gmail.com>
2005-03-15 09:09 ` Christoph Haller <ch@rodos.fzk.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox