|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Save your reputation with your customers. Learn how you can have embedding success with Advantage Database Server (ADS). |
|
#1
|
|||
|
|||
|
Concatenating SQL row data into a string
Hi
I have a table that has (lets say) the following data Field 1 Field 2 1 abc 1 def 1 ghi 2 jkl 2 mno 2 pqr I want the results from this table in this format with an SQL query Field 1 Field 2 1 abc, def, ghi 2 jkl, mno, pqr ..Basically I want to get the concatenation of the row values in Field 2 grouped by Field 1. I'd like to know how to accomplish this without using cursors. Thanks in advance |
|
#2
|
||||
|
||||
|
why can't you use cursors?
__________________
My brain cells are like a storm trooper's armor: useless |
|
#3
|
|||
|
|||
|
My table size is very huge and using cursors may be a bit inefficient.
I guess I can accomplish this without using cursors if I use a User-defined function in the select query and use group by in it that is something like.. select dbo.GetField2Data(Field1), Field1 from <tablename> group by Field1 and in the UDF I can say something like declare @var1 as varchar(1000) declare @ret as varchar(2000) Set @var1='' select @var1 = @var1 + Field1 + ',' from <tablename> if len(@var1) >0 Set @ret = substring(@var1,1,len(@var1)-1) else set @ret = '' return @ret but the problem again with that is that it may be inefficient. Also I cannot use User Defined Functions with temporary tables which I do need to use anyways. I was wondering if there is any variant of the sum function (as in numeric values) for string values. Any ideas ?? |
|
#4
|
||||
|
||||
|
the way it sounds i think that using cursors would be more efficient than creating a new table and trying to handle the UDT.
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Concatenating SQL row data into a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|