Simulating group_concat MySQL function in Microsoft SQL Server 2005?

I’m trying to migrate a MySQL-based app over to Microsoft SQL Server 2005 (not by choice, but that’s life).

In the original app, we used almost entirely ANSI-SQL compliant statements, with one significant exception — we used MySQL’s group_concat function fairly frequently.

group_concat, by the way, does this: given a table of, say, employee names and projects…

SELECT empName, projID FROM project_members;

returns:

ANDY   |  A100
ANDY   |  B391
ANDY   |  X010
TOM    |  A100
TOM    |  A510

… and here’s what you get with group_concat:

SELECT 
    empName, group_concat(projID SEPARATOR "https://stackoverflow.com/") 
FROM 
    project_members 
GROUP BY 
    empName;

returns:

ANDY   |  A100 / B391 / X010
TOM    |  A100 / A510

So what I’d like to know is: Is it possible to write, say, a user-defined function in SQL Server which emulates the functionality of group_concat?

I have almost no experience using UDFs, stored procedures, or anything like that, just straight-up SQL, so please err on the side of too much explanation 🙂

13 Answers
13

Leave a Comment