Solved

Contatenate string

Posted on 2003-11-12
7
324 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now