473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cast text to bytea

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I want to change a column from text to bytea; since it seems that alter
table can't change the column type, i have to add a temporary column and
copy the data from the old one to the new, delete the old and rename the
new.
But it seems that Postgres can't cast text to bytea:

odem=> UPDATE forum_gtree SET gid2=gid::bytea;
ERROR: Cannot cast type text to bytea
The same with casting only:

odem=> SELECT 'abc'::text::bytea;
ERROR: Cannot cast type text to bytea
odem=> SELECT '\000'::text::bytea;
ERROR: Cannot cast type text to bytea
How can I do this?

encode/decode seems to handle only bytea data:

http://www.postgresql.org/docs/curre...ns-string.html
And in http://www.postgresql.org/docs/curre...pe-binary.html I
also found no solution -- perhaps I missed something?
Thanks && Ciao
Alvar
- --
** Alvar C.H. Freude -- http://alvar.a-blast.org/
** Berufsverbot? http://odem.org/aktuelles/staatsanwalt.de.html
** ODEM.org-Tour: http://tour.odem.org/
** Informationsgesellschaft: http://www.wsis-koordinierungskreis.de/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (FreeBSD)

iD8DBQE/zZ/hOndlH63J86wRAmvoAJ4jwJp5R0nrmf8FzG9O599ED/8eCgCeOO2L
RpqleJNIv5RKvorYzh5+lo4=
=7mYs
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #1
2 21691
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

- -- Alvar Freude <al***@a-blast.org> wrote:
But it seems that Postgres can't cast text to bytea:

odem=> UPDATE forum_gtree SET gid2=gid::bytea;
ERROR: Cannot cast type text to bytea


I tryed it with DECODE:

odem=> UPDATE forum_gtree SET gid2=(DECODE(gid::text, 'escape'));
ERROR: decode: Bad input string for type bytea

hmmm ...
The text columns contains values from \x01 to \xff -- everything exept the
null byte.
So, hmmm, it seems I'm faster writing a small piece of Perl to convert this
then searching a solution ... ;)
Ciao
Alvar

- --
** Alvar C.H. Freude -- http://alvar.a-blast.org/
** Berufsverbot? http://odem.org/aktuelles/staatsanwalt.de.html
** ODEM.org-Tour: http://tour.odem.org/
** Informationsgesellschaft: http://www.wsis-koordinierungskreis.de/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (FreeBSD)

iD8DBQE/za68OndlH63J86wRAmE/AKCCaQU/YDFNv6fnri6D7I6Q74ts7QCfcqE1
S+I44j/Lx3mots/pt5EWBJE=
=LVOG
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 12 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

- -- Alvar Freude <al***@a-blast.org> wrote:

odem=> UPDATE forum_gtree SET gid2=(DECODE(gid::text, 'escape'));
ERROR: decode: Bad input string for type bytea


the solution is: backslashes in the text column are interpreted as escapes.
So: they must be marked escaped first. Replacing "\" with "\\" does this:

UPDATE table SET bytea_col =
(decode(replace(text_col, '\\', '\\\\'), 'escape'));
This seems to work and looks like there are the correct values in the new
column ...
Ciao
Alvar

- --
** Alvar C.H. Freude -- http://alvar.a-blast.org/
** Berufsverbot? http://odem.org/aktuelles/staatsanwalt.de.html
** ODEM.org-Tour: http://tour.odem.org/
** Informationsgesellschaft: http://www.wsis-koordinierungskreis.de/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (FreeBSD)

iD8DBQE/zePPOndlH63J86wRAmEaAJ9LAWvLFwekca58BTWYYBuPgWrawA CeJRjl
POrmVVWkT0+XmF7clqBtKlA=
=wH3M
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mPath Records | last post by:
Hello, I hope someone can help me with this problem - I'm getting nowhere fast and have been working on it for a week. All I'm trying to do is upload an image, store the image in a bytea...
4
by: Morten Goodwin Olsen | last post by:
Python, PostgreSQL and bytea Hi I am having a bit of trouble inserting images into a postgres database using python. The images should be stored in a bytea field. I have tried both with an odbc...
1
by: Baldur Norddahl | last post by:
Hi, I need to do something like this: select * from sms where message ilike 'foo%'; Message is a bytea field with UTF-8 content. The charset is actually not important for me, it would be...
0
by: Alvar Freude | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, the following I posted already on pgsql-bugs -- perhaps someone has a good workaround or fix or can say me that I'm wrong? There seems to...
3
by: btober | last post by:
Anyone have a good function for byte-by-byte XOR of text or varchar data (or does one exist already in pg which I missed in the documentation)? ~Berend Tober ...
2
by: Carlos | last post by:
Do I need to use the -b option in pg_dump to dump bytea fields? For a while now I have been routinely using the -b option in pg_dump to back up, restore, and copy my databases because I thought...
7
by: Dennis Gearon | last post by:
when bytea, text, and varchar(no limit entered) columns are used, do they ALWAYS use an extra table/file? Or do they only do it after a certain size of input? Also, if I wanted to put a *.pdf...
0
by: Peter Wang | last post by:
I have a function and am using cursor. All the columns I select in the cursor clause are BYTEA datatype. I need to compare the after-fetch-value for two BYTEA columns which is temp2 and temp3...
0
by: Oliver Nolden | last post by:
Hi everyone, I have a table with a bytea-column: CREATE TABLE picture( id int primary key, preview bytea NOT NULL); How can I insert a value in the bytea-column 'preview'? The function...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.