X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4E28056460 for ; Tue, 15 Mar 2005 09:09:14 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61331-02 for ; Tue, 15 Mar 2005 09:09:11 +0000 (GMT) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by svr1.postgresql.org (Postfix) with SMTP id 88B2856583 for ; Tue, 15 Mar 2005 09:09:09 +0000 (GMT) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Mar 15 10:09:07 2005 +0100 Received: from rodos.fzk.de (resy6.fzk.de [141.52.68.10]) by resy5.fzk.de (8.12.7/8.12.7/SuSE Linux 0.6) with ESMTP id j2F995sG021500; Tue, 15 Mar 2005 10:09:05 +0100 Message-ID: <4236A630.E6952C1B@rodos.fzk.de> Date: Tue, 15 Mar 2005 10:09:04 +0100 From: Christoph Haller X-Mailer: Mozilla 4.77 [en] (X11; U; HP-UX B.10.20 9000/780) X-Accept-Language: en MIME-Version: 1.0 To: Lance Peterson Cc: pgsql-sql@postgresql.org, Michael.Moran@igt.com Subject: Re: How to cast VARCHAR to BYTEA and vice-versa? References: <4235683A.6D2CA65A@rodos.fzk.de> Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS - amavis-milter (http://www.amavis.org/) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.026 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/212 X-Sequence-Number: 20691 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 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 > >