concatinate ids

  • hi

    i have many rows containing a specific id

    example   row1:11

                  row2:12

                   row3:13

     

    i want to get these values in a string seperated with comma like :  11,12,13

    how can i do it without a cursor?even without temp tables

    plz help


    If something's hard to do, then it's not worth doing.

  • Hi

    declare @var varchar(100)

    set @var = ''

    select @var = @var + cast(col1 as varchar(3)) + ',' from table

    -- to remove the trailing comma

    set @var = left(@var ,(len(@var )-1))

    select @var

    "Keep Trying"

  • See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

     


    N 56°04'39.16"
    E 12°55'05.25"

  • i use this:

     

    CREATE

    FUNCTION [dbo].[GetList]()

    RETURNS

    Varchar(20)

    AS

    BEGIN

    DECLARE

    @Result Varchar(20)

    SELECT

    @Result = Coalesce(@Result + ',', '') + ID

    From tablename

    RETURN ( @Result )

    END


    If something's hard to do, then it's not worth doing.

  • SQL 2005 - XML way

    select col1, stuff( ( select ','+ col2

    from tab1 t1

    where t2.col1 = t1.col1

    for xml path('')

    ),1,1,'')

    from tab1 t2

    group by col1

    order by col1


    -- Amit



    "There is no 'patch' for stupidity."



    Download the Updated SQL Server 2005 Books Online.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply