Re: PgSQL problem: How to split strings into rows

From: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
To: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: PgSQL problem: How to split strings into rows
Date: 2010-01-21 19:16:14
Message-ID: 20100121201614.585ec848@dawn.webthatworks.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 21 Jan 2010 13:49:45 -0500
Kynn Jones <kynnjo(at)gmail(dot)com> wrote:

> I have a table X with some column K consisting of
> whitespace-separated words. Is there some SELECT query that will
> list all these words (for the entire table) so that there's one
> word per row in the returned table? E.g. If the table X is
>
> K
> ---------------------
> foo bar baz
> quux frobozz
> eeny meeny
> miny moe
>
> ...I want the result of this query to be
>
> foo
> bar
> baz
> quux
> frobozz
> eeny
> meeny
> miny
> moe

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

select (string_to_array('tano pino gino', ' '))[i] from
generate_series(1, 3) s(i);

You'd get the idea... to get the length of the array you've
array_length.

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alan Millington 2010-01-21 19:54:32 Error "invalid byte sequence for encoding UTF8" on insert into BYTEA column
Previous Message Andreas Kretschmer 2010-01-21 19:07:06 Re: PgSQL problem: How to split strings into rows