dbTalk Databases Forums  

AS400 DB2 syntax for concat string in table column?

ibm.software.db2.os400 ibm.software.db2.os400


Discuss AS400 DB2 syntax for concat string in table column? in the ibm.software.db2.os400 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
superdec@gmail.com
 
Posts: n/a

Default AS400 DB2 syntax for concat string in table column? - 09-17-2009 , 06:32 PM






I have one table on AS400 DB2 database something like with three columns...

*FirstName LastName Department*

James Anderson Computer Science
James Anderson Information Technology
James Anderson Information System
Robert McCafee Heath Care
Robert McCafee Biology
Suman Kapoor Compter Science
Suman Kapoor Information Technology
Luke Morety Computer Science
....
....
....



Final table I am looking is look something like.....

*FirstName LastName Department*

James Anderson Computer Science, Information Technology, Information System
Robert McCafee Health Care, Bilology
Suman Kapoor Computer Science, Information Technology
Luke Morety Computer Science
......
......
......


Any idea with AS400 DB2 syntax pls? thanks....

Reply With Quote
  #2  
Old   
MarkWagoner
 
Posts: n/a

Default Re: AS400 DB2 syntax for concat string in table column? - 09-23-2009 , 07:32 AM






I don't think SQL is going to be they best way to do that. IF you can get it to work you will need a select distinct to get the individual names, then self-join this back to the table to get the departments. But since this is going to be a variable number of rows I'm not sure exactly how to to do that.

It would probably be quicker to write a program.

Reply With Quote
  #3  
Old   
Kent Milligan
 
Posts: n/a

Default Re: AS400 DB2 syntax for concat string in table column? - 09-25-2009 , 09:57 AM



Here's an example using a user-defined function.
select name, dept_list(name) FROM(select name from dept_table group by name) as
namelist;


create function dept_list (iname varchar(10))
returns varchar(1000)
language sql
begin
declare templist varchar(1000) default '';

FOR v1 AS clist CURSOR FOR select dept from dept_table where lastname=iname
DO
SET templist = templist || dept || ' ';
END FOR;

RETURN templist;
END;

MarkWagoner wrote:
Quote:
I don't think SQL is going to be they best way to do that. IF you can get it to work you will need a select distinct to get the individual names, then self-join this back to the table to get the departments. But since this is going to be a variable number of rows I'm not sure exactly how to to do that.

It would probably be quicker to write a program.

Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.