Re: Split a string to rows?

From: "Jonathan S(dot) Katz" <jonathan(dot)katz(at)excoventures(dot)com>
To: emilu(at)encs(dot)concordia(dot)ca
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Split a string to rows?
Date: 2013-01-07 19:58:57
Message-ID: 747F3D71-64DE-4697-B5D5-D6476563801F@excoventures.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Jan 7, 2013, at 2:44 PM, Emi Lu wrote:

> Hello,
>
> Is there a function to split a string to different rows?
>
> For example, t1(id, col1)
> values(1, 'a, b, c');
>
> select id, string_split_to_row(col1, ',');
>
> Return:
> =========
> 1, a
> 1, b
> 1, c

You can probably use some combination of "string_to_array" and "unnest"

e.g.

SELECT unnest(string_to_array('a,b,c', ','));

unnest
--------
a
b
c
(3 rows)

If you need a more complex string splitting mechanism, there is the "regexp_split_to_array" function.

Jonathan

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Emi Lu 2013-01-07 20:16:25 Re: Split a string to rows?
Previous Message Steve Crawford 2013-01-07 19:54:31 Re: Split a string to rows?