If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL - Multiple text values in the same field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-05-08, 00:22
age age is offline
Registered User
 
Join Date: Aug 2008
Posts: 2
SQL - Multiple text values in the same field

Hi

Is there any way you can concatenate multiple text values in the one field? The following query....

Code:
select distinct name.id, education.course
from name
inner join education on name.id = education.id
...returns a new record for each course the person has completed. Is there a way that all these values could be concatenated in one field?

Currently something like this is returned.

ID course
111 bachelor of arts
111 bachelor of law
111 master of arts

The following would be ideal.

ID course
111 bachelor of arts, bachelor of law, master of arts

Is this possible?

Thanks in advance for any help
Reply With Quote
  #2 (permalink)  
Old 08-05-08, 00:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
not in ANSI SQL, no

it's trivially easy in MySQL, more difficult in other database systems

for example, in SQL Server, you'd write a user-defined function, such as this --
http://sqlblindman.googlepages.com/c...limitedstrings
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-05-08, 01:00
age age is offline
Registered User
 
Join Date: Aug 2008
Posts: 2
Thanks for that . I modified the code in that link to...

Code:
declare @DelimitedString varchar(500)

Select  @DelimitedString = isnull(@DelimitedString + ', ', '') + education.course
From    education

select distinct name.id, @DelimitedString
from name
inner join education on name.id = education.id
...it does concatenate multiple values in one field, however it includes every value contained in the course field on every record (until it runs over 500 chars ), while I only want the courses that an individual has completed. Should there be a group by in there somewhere?
Reply With Quote
  #4 (permalink)  
Old 08-05-08, 04:09
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by age
I only want the courses that an individual has completed
WHERE clause
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 08-05-08, 04:31
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by r937
not in ANSI SQL, no
Actually, you can do that quite easily with recursive SQL, which is standardized. It can typically be found under "bill of materials".
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 08-05-08, 05:12
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
oh, that's right, Knut, recursive CTE

can you show an example please?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 08-05-08, 07:04
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Sure. One example can be found here: http://publib.boulder.ibm.com/infoce.../r0000879.html Instead of multiplying quantities, you can use the CONCAT (or ||) operator to deal with strings.

Note that the SQL standard requires the additional RECURSIVE keyword, i.e. it would have to be "WITH RECURSIVE RPL ( ..."
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 08-12-08, 17:43
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,076
Quote:
Originally Posted by r937
oh, that's right, Knut, recursive CTE

can you show an example please?
See e.g. the threads http://www.dbforums.com/ansi-sql/1612892-query-special-transpose-merge-data.html#post6248263, Concatenate column values from multiple rows and How to concat several lines into one
Note that you need to specify the concatenation order in some way or the other, e.g. through a third column (or in the example it's alphabetic on the second column).
(Aggregate SUM, COUNT etc. are order-indifferent, CONCAT isn't, that's why there is no aggregate CONCAT function.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On