Solved

SQL Query help

Posted on 2013-01-08
4
343 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…

864 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

23 Experts available now in Live!

Get 1:1 Help Now