How to do GROUP_CONCAT in SQL Server?

Create a comma seperated result in SQL Server

ryoung at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

;WITH a AS ( SELECT u.userId, u.firstName, u.lastName, ( SELECT k.keyword + ',' FROM Keywords k WHERE k.userId = u.userId ORDER BY k.keyword FOR XML PATH('') ) AS [keyword] FROM Users u ) SELECT a.userId, a.firstName, a.lastName, -- strip off trailing comma LEFT( a.keyword, LEN(a.keyword)-1 ) AS keyword FROM a

Sean

You can create a new sql aggregate function concat to concatenate texts and use if in you query: select userId, firstName, Lastname, concat(Keyword) from Users group by userId, firstName, Lastname; Follwoing link shows an example on how to create the concat funcion. http://msdn.microsoft.com/en-us/library/ms182741.aspx

Raihan

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.