Lists: | pgsql-general |
---|
From: | A E <cooljoint(at)yahoo(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Passing a comma delimited list to a function |
Date: | 2004-01-03 18:11:55 |
Message-ID: | 20040103181155.67492.qmail@web12103.mail.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Hi,
I was wondering if there was a function that handles list elements of a comma delimited list? I need to be able to pass values as a comma delimited list, count the number of values, and process the value of each.
Did not think it was very efficient to loop through the contents of the list finding delimiters.
TIA
Alex
From: | Joe Conway <mail(at)joeconway(dot)com> |
---|---|
To: | A E <cooljoint(at)yahoo(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Passing a comma delimited list to a function |
Date: | 2004-01-03 23:10:11 |
Message-ID: | 3FF74BD3.1030302@joeconway.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
A E wrote:
> I was wondering if there was a function that handles list elements of
> a comma delimited list? I need to be able to pass values as a comma
> delimited list, count the number of values, and process the value of
> each.
You didn't mention a version, but in 7.4 you can do this:
create or replace function unravel(text) returns setof int as '
declare
v_list alias for $1;
v_delim text := '','';
v_arr text[];
begin
v_arr := string_to_array(v_list, v_delim);
for i in array_lower(v_arr, 1)..array_upper(v_arr, 1) loop
return next v_arr[i]::int;
end loop;
return;
end;
' language plpgsql;
regression=# select * from unravel('1,2,3,4,5');
unravel
---------
1
2
3
4
5
(5 rows)
HTH,
Joe
From: | "Ezra Epstein" <news-reader(at)prajnait(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Passing a comma delimited list to a function |
Date: | 2004-01-07 05:00:10 |
Message-ID: | 4uOdnUwZt7z2D2aiXTWc-g@speakeasy.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Take a look at:
http://www.postgres.org/docs/current/static/functions-string.html
The split_part() function should do the trick.
== Ezra Epstein
"A E" <cooljoint(at)yahoo(dot)com> wrote in message
news:20040103181155(dot)67492(dot)qmail(at)web12103(dot)mail(dot)yahoo(dot)com(dot)(dot)(dot)
Hi,
I was wondering if there was a function that handles list elements of a
comma delimited list? I need to be able to pass values as a comma delimited
list, count the number of values, and process the value of each.
Did not think it was very efficient to loop through the contents of the list
finding delimiters.
TIA
Alex