By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

retrieve column array values

P: 38
i have a column which contains arrays of variable length. for example one array contains 5 elements, another contains 2 elements, another array 10 elements.
i have 500 records in this column

i want to select all elements of an array then below all elements of another array and so on in a normal column. this is very urgent if some body can help plz....

infact this operation is reverse to insert values from a column into array.

thnx n regards
Jan 20 '07 #1
Share this Question
Share on Google+
1 Reply


P: 4
Do you mean essentially exploding the results into individual rows:

source:

{1,2,3} , {3,4,5,6,7,8} , {9,a,b,c}

desired result :
1
2
3
3
4
5
6
7
8
9
a
b
c

If so you can use an explode array stored procedure like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE OR REPLACE FUNCTION explode_array(in_array anyarray)
  3.   RETURNS SETOF anyelement AS
  4. $BODY$
  5.     SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
  6. $BODY$
  7.   LANGUAGE 'sql' IMMUTABLE;
  8. ALTER FUNCTION explode_array(in_array anyarray) OWNER TO postgres;
  9.  
and call

SELECT explode_array( col1 || col2 || col3 )
FROM Table blah

where col1, col2 and col3 are the arrays in your table. The || operator joins the arrays together first.

Thanks.
Feb 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.