Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query help

Posted on 2013-01-08
4
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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 143

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

604 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