cast bytea to text

Started by Willy-Bas Loosabout 19 years ago5 messagesgeneral
Jump to latest
#1Willy-Bas Loos
willybas@gmail.com

Dear List,

How can i cast bytea to text?
I´ve read about the DECODE function, but my 8.1 backend doesn´t recognize
it.
I´m trying to create an implicit cast using the function:

create or replace function bytea2text(bytea) returns text as
$$
select DECODE($1, 'escape');
$$
language sql strict;

Answer:
ERROR: function decode(bytea, "unknown") does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You may need to
add explicit type casts.
Context: SQL function "bytea2text"

In reply to: Willy-Bas Loos (#1)
Re: cast bytea to text

Willy-Bas Loos wrote:

ERROR: function decode(bytea, "unknown") does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You may
need to
add explicit type casts.
Context: SQL function "bytea2text"

That's because the first parameter of decode() should be of type TEXT,
not bytea:

http://www.postgresql.org/docs/8.1/static/functions-binarystring.html#FUNCTIONS-BINARYSTRING-OTHER

Ray.

---------------------------------------------------------------
Raymond O'Donnell, Director of Music, Galway Cathedral, Ireland
rod@iol.ie
---------------------------------------------------------------

#3Peter Eisentraut
peter_e@gmx.net
In reply to: Willy-Bas Loos (#1)
Re: cast bytea to text

Willy-Bas Loos wrote:

How can i cast bytea to text?
I�ve read about the DECODE function, but my 8.1 backend doesn�t
recognize it.

You want to use encode() in that direction.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/

#4Albe Laurenz
all@adv.magwien.gv.at
In reply to: Willy-Bas Loos (#1)
Re: cast bytea to text

How can i cast bytea to text?
I´ve read about the DECODE function, but my 8.1 backend
doesn´t recognize it.
I´m trying to create an implicit cast using the function:

create or replace function bytea2text(bytea) returns text as
$$
select DECODE($1, 'escape');
$$
language sql strict;

Answer:
ERROR: function decode(bytea, "unknown") does not exist

Try ENCODE instead of DECODE ...

Yours,
Laurenz Albe

#5Willy-Bas Loos
willybas@gmail.com
In reply to: Albe Laurenz (#4)
Re: cast bytea to text

yep, sry it took me a while to answer. It works now.
here´s my code:
-- start code --
create or replace function bytea2text(bytea) returns text as
$$
select ENCODE($1, 'escape');
$$
language sql strict;

create cast (bytea as text)
with function bytea2text(bytea)
as implicit;
-- end code --

thanks!

Willy-Bas Loos

Show quoted text

On 3/1/07, Albe Laurenz <all@adv.magwien.gv.at> wrote:

How can i cast bytea to text?
I´ve read about the DECODE function, but my 8.1 backend
doesn´t recognize it.
I´m trying to create an implicit cast using the function:

create or replace function bytea2text(bytea) returns text as
$$
select DECODE($1, 'escape');
$$
language sql strict;

Answer:
ERROR: function decode(bytea, "unknown") does not exist

Try ENCODE instead of DECODE ...

Yours,
Laurenz Albe