Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

SQL Query help

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
zolf
Asked:
zolf
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
zolfAuthor Commented:
cheers !!
0
 
zolfAuthor Commented:
Can you please explain what is FOR XML PATH ('')) does.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now