-
concatenate all rows
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
-
hi,
U can display it through utl_file.......
ramya
-
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.
-
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
-
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
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"
Click HERE to vist my website!
-
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
-
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
-
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> /
REPLACE(COL1,'')
----------------------------------------
abcd
Tamil
-
Tamil:
Thanks, I was testing with hard coded values to make sure the concept worked, and missed that generalization.
John
-
Thanks
Hi
This Query is working fine.Thanks a lot for your response.
Thanks
RajendranR
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|