To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

HOME 11g Central 10g Central 9i Central 8i Central Oracle News Scripts FAQ OCP Zone Resources Database Journal
  #1  
Old 06-09-2005, 02:40 AM
Rajendran Rajendran is offline
Junior Member
 
Join Date: Jun 2005
Location: Chennai
Posts: 6
Angry 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
Reply With Quote
  #2  
Old 06-09-2005, 04:46 AM
rumcool rumcool is offline
Junior Member
 
Join Date: Sep 2003
Location: india
Posts: 23
hi,

U can display it through utl_file.......
__________________
ramya
Reply With Quote
  #3  
Old 06-09-2005, 07:06 AM
OraBis OraBis is offline
Junior Member
 
Join Date: May 2005
Posts: 10
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.
Reply With Quote
  #4  
Old 06-13-2005, 05:30 AM
Rajendran Rajendran is offline
Junior Member
 
Join Date: Jun 2005
Location: Chennai
Posts: 6
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
Reply With Quote
  #5  
Old 06-13-2005, 06:04 AM
jovery's Avatar
jovery jovery is offline
Ha Ha
 
Join Date: Jan 2002
Location: Up s**t creek
Posts: 1,525
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!
Reply With Quote
  #6  
Old 06-14-2005, 01:18 AM
Rajendran Rajendran is offline
Junior Member
 
Join Date: Jun 2005
Location: Chennai
Posts: 6
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
Reply With Quote
  #7  
Old 06-14-2005, 11:17 AM
John Spencer John Spencer is offline
Junior Member
 
Join Date: May 2005
Location: Toronto Canada
Posts: 57
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
Reply With Quote
  #8  
Old 06-14-2005, 01:14 PM
tamilselvan tamilselvan is offline
Moderator
 
Join Date: May 2000
Location: ATLANTA, GA, USA
Posts: 3,136
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
Reply With Quote
  #9  
Old 06-14-2005, 03:20 PM
John Spencer John Spencer is offline
Junior Member
 
Join Date: May 2005
Location: Toronto Canada
Posts: 57
Tamil:

Thanks, I was testing with hard coded values to make sure the concept worked, and missed that generalization.

John
Reply With Quote
  #10  
Old 06-18-2005, 12:27 AM
Rajendran Rajendran is offline
Junior Member
 
Join Date: Jun 2005
Location: Chennai
Posts: 6
Thanks

Hi
This Query is working fine.Thanks a lot for your response.

Thanks
RajendranR
Reply With Quote
Reply Post New Thread

Bookmarks

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -4. The time now is 11:11 PM.


DBAsupport.com Recent Articles


 » Examining the Oracle 10053 Trace Event Dump File

 » I/O Performance Tuning Tools for Oracle 11gR2

 » Solve Database Performance Tuning Problems

 » Regain Control of Your Oracle Database

 » Oracle Database 11gR2 I/O Performance Tuning

Search DBAsupport:
 


Click Here to Expand Forum to Full Width










Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.