Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 8th, 2006, 01:04 AM
Authorized User
Join Date: Nov 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Comma Separated Column Value thru T-SQL

I have 1 table containing
table 1
c1 c2 c3
1 A 11
1 A 12
2 B 14
2 B 15
2 B 17
3 C 10

From tha above table, I want to return a resultset which seems like below

c1 c2 c3
1 A 11,12
2 B 14,15,17
3 C 10

Kindly give me the SQL Query for the above problem..
Subhendu De
[email protected]
Software Developer
New Delhi, India
Old December 8th, 2006, 08:56 AM
Friend of Wrox
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts


Create a function similar to this...

        DECLARE @oResult VARCHAR(8000)
         SELECT @oResult= COALESCE(@oResult+',','')+CAST(C3 AS VARCHAR(10)
           FROM yourtable WITH (NOLOCK)
          WHERE C2 = @pC2
          ORDER BY C3
         RETURN @oResult
... call it like this...

   FROM yourtable

--Jeff Moden
Old December 12th, 2006, 01:42 PM
Authorized User
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts

Here's a link that discusses the same answer that Jeff has provided:


SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp

Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving comma separated values from column abhishekkashyap27 SQL Server 2005 6 April 21st, 2008 09:27 AM
Comma Separated values aldwinenriquez SQL Server 2005 3 June 1st, 2007 08:52 PM
comma separated list MunishBhatia SQL Server 2000 11 March 21st, 2007 03:56 PM
comma separated list of attributes rjonk XSLT 3 September 27th, 2006 11:52 AM
Import a Comma separated txt file to SQL table arielote C# 2 April 17th, 2006 01:08 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.