MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Save your reputation with your customers. Learn how you can have embedding success with Advantage Database Server (ADS).
  #1  
Old September 25th, 2003, 05:04 PM
kahaji kahaji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 kahaji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old September 29th, 2003, 11:08 AM
don_sparko's Avatar
don_sparko don_sparko is offline
Digitally Challenged
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 280 don_sparko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 14 sec
Reputation Power: 6
why can't you use cursors?
__________________
My brain cells are like a storm trooper's armor: useless

Reply With Quote
  #3  
Old September 30th, 2003, 06:47 AM
kahaji kahaji is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 kahaji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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 ??

Reply With Quote
  #4  
Old September 30th, 2003, 08:44 AM
don_sparko's Avatar
don_sparko don_sparko is offline
Digitally Challenged
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 280 don_sparko User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 14 sec
Reputation Power: 6
the way it sounds i think that using cursors would be more efficient than creating a new table and trying to handle the UDT.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Concatenating SQL row data into a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway