Re: Split a string to rows? - Mailing list pgsql-sql

From Jonathan S. Katz
Subject Re: Split a string to rows?
Date
Msg-id 747F3D71-64DE-4697-B5D5-D6476563801F@excoventures.com
Whole thread Raw
In response to Split a string to rows?  (Emi Lu <emilu@encs.concordia.ca>)
Responses Re: Split a string to rows?  (Emi Lu <emilu@encs.concordia.ca>)
List 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
--------abc
(3 rows)

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

Jonathan


pgsql-sql by date:

Previous
From: Steve Crawford
Date:
Subject: Re: Split a string to rows?
Next
From: Emi Lu
Date:
Subject: Re: Split a string to rows?