Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if i am going to select Records


select id from tbl_records

the result like this

1
2
3
4
5
6
7

how do i get like this

row into column


1 2 3 4 5 6 7
Posted

try this
SQL
SELECT STUFF((SELECT ',' + cast(ID as varchar) AS [text()]
  FROM tbl_records  FOR XML PATH('') ), 1, 1, '' )

its a comma separated if dont want you can remove it ;)
 
Share this answer
 
v2
Comments
King Fisher 23-Jan-14 23:19pm    
this is a good one.. thanks
PIVOT will help you.!
See what is PIVOT[^] and it's examples[^].
 
Share this answer
 
v2
Just to add, here[^] is my article on pivot, for xml, and the other options you have to do what you want.

It's especially important to note that the 'FOR XML' option is the only one that works if you don't know before you start, what your list of values you want pulled out is.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900