Hi
My question is "how to concatenate all rows as a single row in a column in oracle?".For example consider a table "test" which having 1 column "col1".if the values of the column col is let it be a,b,c,d,e.
select * from test;
a
b
c
d
e
I need the output in the folowing format.
abcde.
Please help me.Thanks in Advance.
SQL> create or replace function my_func return varchar2 is
2 type rc is ref cursor;
3 v_str varchar2(20);
4 v_str_tmp varchar2(20);
5 c rc;
6 begin
7 open c for 'select * from test';
8 loop
9 fetch c into v_str_tmp;
10 exit when c%notfound;
11 v_str := v_str||v_str_tmp;
12 end loop;
13 return v_str;
14 end;
15 /
SQL> select * from t1;
COL1
----
a
b
c
d
e
SQL> set serveroutput on
SQL> declare
2> v_str varchar2(100);
3> begin
4> for c in (select col1 from t1) loop
5> v_str := v_str||c.col1;
6> end loop;
7> dbms_output.put_line(v_str);
8> end;
9> /
abcde
HTH
Jim
Oracle Certified Professional "Build your reputation by helping other people build theirs."
"Sarcasm may be the lowest form of wit but its still funny"
Ordinarily, I would suggest searching Ask Tom for stragg. However, since you cannot or will not use the best approach.
This works for your supplied data set, and may be generalizable to other data sets, depending on the real structure of you data. Also, it requires Oracle 9.
Code:
SQL> SELECT * FROM t;
COL1
--------------------
a
b
c
d
e
SQL> SELECT REPLACE(col1, ' ')
2 FROM (SELECT SYS_CONNECT_BY_PATH(col1,' ') col1, level
3 FROM t
4 START WITH col1 = 'a'
5 CONNECT BY PRIOR col1 < col1
6 ORDER BY level DESC)
7 WHERE rownum = 1;
REPLACE(COL1,'')
------------------
abcde
Bookmarks