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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 30
hyperlink data type in SQL 3 27
Linked Server Issue with SQL2012 3 26
sql server tables from access 18 19
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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