Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
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
Edited 28-Dec-12 3:11am
v3
Comments
Sandeep Mewara at 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__ at 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 at 28-Dec-12 9:26am
   
Good example, 5.
__TR__ at 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__ at 28-Dec-12 9:27am
   
My 5!
Mika Wendelius at 28-Dec-12 9:28am
   
Thanks :D
Espen Harlinn at 29-Dec-12 7:39am
   
Nice :-D
Mika Wendelius at 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
0 OriginalGriff 300
1 Maciej Los 180
2 Richard MacCutchan 115
3 arvind mepani 104
4 Tino Fourie 92


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