TSQL: Concatenate Column from Multiple Rows into String

24 בינואר 2012

Ok , So I had a problem related to concatenation of a single Column into one ( same ) column when using GROUP BY .
The Problem came from a Reporting Services perspective and there might be other solutions ( like coding in VB.NET in the report )

I have found a very nice samples :

Option 1:

use Northwind

declare @CategoryList varchar(1000)
set @CategoryList ="
select @CategoryList = @CategoryList + ' , ' + CategoryName from Categories

select 'Results = ' + @CategoryList

Option 2:

use Northwind

declare @CategoryList varchar(1000)
select @CategoryList = coalesce(@CategoryList + ', ', ") + CategoryName from Categories

select 'Results = ' + @CategoryList

In the end I used option 2 as part of a UserDefiendFunction .

The manipulation was that the of the “GROUP BY” was sent to the UDF to use in it’s select “where” , so it could return the filtered row concatenated. And that UDF was called as part of the grand SELECT.

NOTE: this is a developer solution , a DBA might have a better solution 😉

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

5 תגובות

  1. Bharat28 בפברואר 2012 ב 14:11

    I think bgnilgog started and took off with people just writing about their personal lives, and it is later in the evolution that we see a lot more specialization. It doesn't make sense to me to want to kill off personal blogs altogether.

    הגב
  2. Fullerton22 ביולי 2012 ב 7:33

    What's up, all the time i used to check webpage posts here early in the daylight, because i like to find out more and more.

    הגב
  3. nipreinee15 בינואר 2013 ב 3:07

    I'd need to check with you here. Which just isn't some thing I in most cases do! I delight in reading a post that can make persons think. Also, thanks for allowing me to comment!

    [url=http://throwbacknfljerseys.beep.com/]Throwback NFL Jerseys[/url]

    http://nfljerseys2012.joomla-host.org

    הגב
  4. http://www.pacostacosonline.com22 ביולי 2013 ב 16:54

    Leaf Qin at this time also and emperor just ice son, control black be covered with cloud, fall together at the huge wood Gang of Shao up.

    הגב
  5. Cashin11 באוקטובר 2013 ב 1:09

    Is it first-rate to place part of this on my webpage if I publish a
    reference point to this webpage

    הגב