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

How to cast VARCHAR to BYTEA and vice-versa?

From
"Moran.Michael"
Date:
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


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

From
Tom Lane
Date:
"Moran.Michael" <Michael.Moran@IGT.com> writes:
> How do I cast VARCHAR to BYTEA?

I think it would work to create a cast WITHOUT FUNCTION, assuming that
the semantics you want is that the exact bytes in the varchar datum
become the bytes in the bytea (no encoding or backslashing conversions).
        regards, tom lane


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

From
"Moran.Michael"
Date:
Thank you, Tom.
Yes, the exact bytes in the varchar datum (without encoding) is what I would
like to become the bytes in the BYTEA.
So, how do I create a "cast WITHOUT FUNCTION" as you mention below? I assume
plpgsql is required, right?
Is there anyway this can be done in a VIEW without having to use plpgsql in
a Function (a.k.a. stored procedure)?
Is there no simple cast or conversion method I can call that does this for
me?
Best Regards,
Michael Moran
 _____  

From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
Sent: Tue 3/8/2005 9:18 AM
To: Moran.Michael
Cc: pgsql-hackers@postgresql.org
Subject: Re: [HACKERS] How to cast VARCHAR to BYTEA and vice-versa? 



"Moran.Michael" <Michael.Moran@IGT.com> writes: 
> How do I cast VARCHAR to BYTEA? 

I think it would work to create a cast WITHOUT FUNCTION, assuming that 
the semantics you want is that the exact bytes in the varchar datum 
become the bytes in the bytea (no encoding or backslashing conversions). 
                       regards, tom lane 



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

From
Alvaro Herrera
Date:
On Tue, Mar 08, 2005 at 02:16:52PM -0800, Moran.Michael wrote:

> Yes, the exact bytes in the varchar datum (without encoding) is what I would
> like to become the bytes in the BYTEA.
>  
> So, how do I create a "cast WITHOUT FUNCTION" as you mention below? I assume
> plpgsql is required, right?

Did you check the reference page for CREATE CAST already?

-- 
Alvaro Herrera (<alvherre[@]dcc.uchile.cl>)
"Por suerte hoy explotó el califont porque si no me habría muertode aburrido"  (Papelucho)


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

From
Christopher Kings-Lynne
Date:
>>How do I cast VARCHAR to BYTEA?
> 
> 
> I think it would work to create a cast WITHOUT FUNCTION, assuming that
> the semantics you want is that the exact bytes in the varchar datum
> become the bytes in the bytea (no encoding or backslashing conversions).

Are pg_escape_bytea and pg_unescape_bytea available from the backend as 
well as libpq?

Chris