Re: Converting value to array

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Sean Davis <sdavis2(at)mail(dot)nih(dot)gov>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Converting value to array
Date: 2005-02-05 16:41:47
Message-ID: 20050205164147.GA23116@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Sat, Feb 05, 2005 at 11:12:09AM -0500, Sean Davis wrote:
>
> I have a column in my table block_sizes(varchar) that looks like:
>
> 12,23,
> 234,23,
> 78,64,28,
>
> i.e., comma-separated integer values (and the included trailing comma).
> I would like to convert these each to an array and store in another
> column.

Have you tried string_to_array()?

http://www.postgresql.org/docs/8.0/static/functions-array.html

CREATE TABLE foo (
strcol text,
arraycol integer[]
);

INSERT INTO foo (strcol) VALUES ('12,23,');
INSERT INTO foo (strcol) VALUES ('234,23,');
INSERT INTO foo (strcol) VALUES ('78,64,28,');

UPDATE foo SET arraycol = string_to_array(rtrim(strcol, ','), ',')::integer[];

SELECT * FROM foo;
strcol | arraycol
-----------+------------
12,23, | {12,23}
234,23, | {234,23}
78,64,28, | {78,64,28}
(3 rows)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2005-02-05 17:10:36 Re: Converting value to array
Previous Message Sean Davis 2005-02-05 16:12:09 Converting value to array