Solved

SQL Query help

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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

687 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