Solved

SQL Query help

Posted on 2013-01-08
4
273 Views
Last Modified: 2013-01-09
HEllo there,

How can i have all the output of the result in this format record1,record2,record3.
the query i am running is SELECT name FROM username.now i want all of the name to be like this in the result name1,name2,name3. i am using MSSQL 2008.

cheers
Zolf
0
Comment
Question by:zolf
  • 2
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38757912
one possibility is this:
declare @res varchar(max)
set nocount on
select @res = coalesce(@res + ',', '') + name from username
select @res user_names

Open in new window


another one:
SELECT STUFF(
             (SELECT ',' + name FROM username FOR XML PATH (''))
             , 1, 1, '') 

Open in new window

0
 

Author Closing Comment

by:zolf
ID: 38757924
cheers !!
0
 

Author Comment

by:zolf
ID: 38757927
Can you please explain what is FOR XML PATH ('')) does.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38757970
please run these statements to "see" what it does:
SELECT ',' + name FROM username FOR XML PATH ('')
SELECT name FROM username FOR XML PATH
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
while loop in html mail format 5 34
SQL Help - 12 42
Report Builder 9 31
SQL JOIN + SUBQUERY? 3 14
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now