Create a comma seperated result in SQL Server
-
Possible Duplicate: http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-ms-sql-server-2005 http://stackoverflow.com/questions/1621747/concat-field-value-to-string-in-sql-server There are 2 tables involved, Users and Keywords. Users userId firstName lastName ... Keywords userId keyword If I run this query: select u.userId, u.firstName, u.lastName, k.keyword from Users as u inner join Keywords as k on u.userId = k.userId I get something like this: What I want is this: http://www.sqlprof.com/blogs/sqldev/archive/2008/03/31/how-to-list-multiple-rows-of-data-on-same-line.aspx but I can't seem to get it to work right.
-
Answer:
SELECT u.userId, u.firstName, u.lastName, (select keyword +', ' from Keywords tk where tk.userid= u.userid for xml path ('') ) as keyword FROM Users AS u Sample query to test: declare @Users as table( userid int, firstname varchar(20), lastname varchar(20) ) declare @Keywords as table( userid int, keyword varchar(20) ) insert into @Users values( 1, 'Bob','Sanders' ), ( 2, 'Jamie','Smith' ) insert into @Keywords values (1,'Cars'), (1,'Pizza'), (2,'Tomato'), (2,'Blah') SELECT u.userId, u.firstName, u.lastName, (select keyword +', ' from @Keywords tk where tk.userid= u.userid for xml path ('') ) as keyword FROM @Users AS u Returns: userId firstName lastName keyword 1 Bob Sanders Cars, Pizza, 2 Jamie Smith Tomato, Blah,
ryoung at Stack Overflow Visit the source
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
Related Q & A:
- Can I create a second filestream container on an existing SQL Server 2008 database without going offline?Best solution by Database Administrators
- What is the best practice for free space for a SQL server database drive?Best solution by Database Administrators
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- How to restrict access to a sql server database from Azure?Best solution by Stack Overflow
- How to change column data's as a separate column wise format in a SQL Server?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.