Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    1

    Unanswered: combine multiple rows into one row

    Hi
    i have a table called tbGroup with multiple data and i want the data to be in one row with ',' to separate the data. hope someone can help me.appreciate it.

    tbGroup
    food
    foodforbeverage
    beverage

    tbGroup
    food,foodforbeverage,beverage

  2. #2
    Join Date
    Mar 2009
    Posts
    349
    IF OBJECT_ID('tempdb..#tbGroup') is not null
    BEGIN
    DROP TABLE #tbGroup
    END

    SELECT 'food' as Homework
    INTO #tbGroup
    UNION
    SELECT 'foodforbeverage'
    UNION
    SELECT 'beverage'

    DECLARE @Blah as varchar(100)

    SET @Blah = ''

    SELECT @Blah = @Blah + Homework + ', '
    FROM #tbGroup

    SELECT @Blah

    IF OBJECT_ID('tempdb..#tbGroup') is not null
    BEGIN
    DROP TABLE #tbGroup
    END

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •