Solved

Contatenate string

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

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
automatically.

0
Comment
Question by:pwang1973
  • 4
  • 2
7 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9733829
0
 
LVL 75

Expert Comment

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

Returns varchar(1000)

As
Begin

Declare      @List varchar(1000)

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

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

Return @List

End

Anthony
0
 
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

Thanks,
Anthony
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:pwang1973
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?

Thanks
Pat
0
 
LVL 75

Accepted Solution

by:
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:
http://www.experts-exchange.com/Web/Web_Languages/Q_20735955.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_20739291.html

This question can be solved using Full-Text Search:
http://www.experts-exchange.com/Databases/Q_20753054.html
But I suggest you post a message in Community Support to have it deleted. See here:
Nobody answered my question. What do I do?
http://www.experts-exchange.com/help/closing.jsp#5

Thanks,
Anthony
0
 
LVL 50

Expert Comment

by:Lowfatspread
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?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9740261
Lowfatspread,

I think you may have posted in the wrong thread.

Anthony
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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 …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

840 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