Contatenate string

Posted on 2003-11-12
Last Modified: 2008-03-10
Is there a way you can concatenate string together in a group.

select max(string) from table group by KEYFIELD
is legal,

but I want to return a string list separated by ","
like 'XXX', 'XXX' for key field.

I can do it using a cursor, but can we write a function so it can be returned

Question by:pwang1973
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 50

Expert Comment

ID: 9733829
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9733923
CREATE Function udf_GetList (@KEYFIELD int)

Returns varchar(1000)


Declare      @List varchar(1000)

Set @List = ''
Select      @List = @List + Value + ', '
From      Table1

-- Len:  Excludes trailing blanks.
If Len(@List) > 0
   Set @List = Left(@List, Len(@List) - 1)

Return @List


LVL 75

Expert Comment

by:Anthony Perkins
ID: 9733928
Also, please maintain these old oopen questions:

1 09/11/2003 50 ASP COM+ component SESSION VARIABLE.  Open Web Languages
2 09/15/2003 155 ACTIVEX - VB control validateEvent  Open VB Controls
3 09/30/2003 50 A utlity to search all fields in all tab...  Open Databases

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.


Author Comment

ID: 9734085
Thanks Anthony:

I tried and it works, the only question left is that I try to create a function with table Name passed in as a paramter, but inside function you can not use the exec command. Is there a workaround for it?

LVL 75

Accepted Solution

Anthony Perkins earned 125 total points
ID: 9734251
>> but inside function you can not use the exec command.<<
That is correct. I do not believe you can use the sp_executesql, either.  In any case using dynamic SQL should only be done as  a last resort.  

>>Is there a workaround for it?<<
Potentially how many distinct tables can be passed in?

Finally, please do not forget your old open questions:
These questions can be deleted as there were no contributions:

This question can be solved using Full-Text Search:
But I suggest you post a message in Community Support to have it deleted. See here:
Nobody answered my question. What do I do?

LVL 50

Expert Comment

ID: 9738536
not that i'd advise using it but would the adding a remote server for the current server
and accessing the tables via openquery trick work in this scenario?
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9740261

I think you may have posted in the wrong thread.


Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL 2012 AOG and SQL2014 AOG 76 61
push and Pull replication 31 48
access to sql migration 5 24
SQL Server In place upgrade from 2012 to 2014 12 22
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question