Click here to Skip to main content
13,049,814 members (75,204 online)
Rate this:
 
Please Sign up or sign in to vote.
hi ,

I have one table like
create table test(id int)
insert into test(101),(102),(103).....

now my expected output is:
test
101,102,103

I know one method to resolve my problem :
decare @id varchar(100)
select @id=coalsce(@id,'')+cast(id as varchar) from test
select @test


but the above method is not suitable to my requirement
so i need to resolve it with normal select statement i.e without using declared variable
Posted 28-Dec-12 2:47am
Updated 28-Dec-12 3:11am
v3
Comments
Sandeep Mewara 28-Dec-12 8:59am
   
Your question is not clear even after an example. Please re-word and try to share what are you trying to do?
__TR__ 28-Dec-12 9:15am
   
Comment from OP
"hi sandeep where you find diffuclty in my post. no i added some more points just check it once"
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try
create table #test(id int)
insert into #test(id) VALUES (101)
insert into #test(id) VALUES (102)
insert into #test(id) VALUES (103)
 
SELECT SUBSTRING(
(
SELECT ',' + CAST(Id AS VARCHAR) FROM #Test
FOR XML PATH('')), 2,10000) AS Csv
 
DROP TABLE #test
  Permalink  
Comments
Mika Wendelius 28-Dec-12 9:26am
   
Good example, 5.
__TR__ 28-Dec-12 9:28am
   
Thank you.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Probably the easiest way to select values into a comma separated list is to use FOR XML clause. For example have a look at this article: Get Column values as comma seperated string [^].

If you need a lot more functionality, you can even consider creating a custom aggregate. See chapter Using Multiple Parameters, Concatenate in Custom Aggregates in SQL Server[^]
  Permalink  
Comments
__TR__ 28-Dec-12 9:27am
   
My 5!
Mika Wendelius 28-Dec-12 9:28am
   
Thanks :D
Espen Harlinn 29-Dec-12 7:39am
   
Nice :-D
Mika Wendelius 29-Dec-12 7:53am
   
Thanks :D
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

my actual requirement is differnet

finaly i solved by taking the above examples

my final solution is

select
    LEFT(SubGroupId ,LEN(SubGroupId ) - 1)
from
    (
    select
        CONVERT(varchar(max),
            (
                select SubGroupId  as [text()], ',' as [text()]
 

                from CMSClientSubGroup  csg WITH(NOLOCK)
            INNER JOIN USER_ACCOUNT ua WITH(NOLOCK)
            --  on u.user_id=ua.user_id
            --INNER JOIN CMSClientSubGroup csg WITH(NOLOCK)
                on csg.SubGroupId=ua.account_id
            --WHERE  ua.USER_ID =1
                for xml path('')
 
            )
 
        )as result
    ) s
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100