Any query experts ??? Very urgent pls!!!

  • Hi friends,

    Table

    Id    value

    --    -----

    1     a

    1     b

    1     c

    2     d

    I need a query to print out value as if I pass id as 1

    field1

    ------

    a,b,c

    Using only query not SP.

    Thanks in advance

  • declare @var nvarchar(150)

    set @var = ''

    select @var = @var + value + "," from table  where id = 1

    print @var




    My Blog: http://dineshasanka.spaces.live.com/

  • declare @value varchar(100)

    select @value =coalesce(@value +',','')+value from t where id =1

    select @value


    Helen
    --------------------------------
    Are you a born again. He is Jehova Jirah unto me

  • Helen's suggestion works but it is an undocumented feature that may not continue with future versions of SQL Server.  My suggestion is to create a function that loops through the records and returns a string of the values.

    If the phone doesn't ring...It's me.

  • I don't think this is undocumented. This technique is called aggregate concatenation and widely used. However, there do exists problems with it which are described here:

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;287515

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

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

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