Skip to content
Advertisement

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…

returns:

… and here’s what you get with group_concat:

returns:

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 🙂

Advertisement

Answer

No REAL easy way to do this. Lots of ideas out there, though.

Best one I’ve found:

Or a version that works correctly if the data might contain characters such as <

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement