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.
Thanks & Regards
RajendranR
06-09-2005, 04:46 AM
rumcool
hi,
U can display it through utl_file.......
06-09-2005, 07:06 AM
OraBis
You can write your own function to do that :
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 /
Function created.
SQL> select my_func() from test where rownum < 2;
MY_FUNC()
----------
abcde
Hope this helps.
06-13-2005, 05:30 AM
Rajendran
Hi
First Thanks for ur reply.I need say one thing. I shoul not use function or procedure at all. i hve to do it by a single query.
Thanks
Rajendran
06-13-2005, 06:04 AM
jovery
How about
Code:
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
06-14-2005, 01:18 AM
Rajendran
Hi Jim
Thanks for your reply.But i need to do this one in a single query.not using user defined function,user defined procedure.Thanks
for help.
Thanks
Rajendran
06-14-2005, 11:17 AM
John Spencer
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
HTH
John
06-14-2005, 01:14 PM
tamilselvan
Thanks John.
Slight variation of John's SQL statement.
PHP Code:
1 SELECT REPLACE(col1, ' ')
2 FROM (SELECT SYS_CONNECT_BY_PATH(col1,' ') col1, level
3 FROM t1
4 START WITH col1 = (select min(col1) from t1) -- 'a'
5 CONNECT BY PRIOR col1 < col1
6 ORDER BY level DESC)
7* WHERE rownum = 1
SQL> /