8

Possible Duplicate:
Concatenate many rows into a single text string?

I have a query

SELECT city FROM cityTable

it returns

delhi
faridabad
haryana
mathura
kerla

I just want result in transpose manner something like

delhi | faridabad | haryana | mathura | kera

How is it possible?

Community
  • 1
  • 1
RAKESH HOLKAR
  • 2,127
  • 5
  • 24
  • 42

3 Answers3

2
SELECT STUFF (
 (SELECT N', ' + city FROM CityTable FOR XML PATH(''),TYPE)
  .value('text()[1]','nvarchar(max)'),1,2,N'')

Concatenate many rows into a single text string?

Community
  • 1
  • 1
Darren
  • 68,902
  • 24
  • 138
  • 144
  • 1
    Thanks sir, But i want just transpose of a column result. I mean, result should return in single Row and with separate column. Is it possible??? – RAKESH HOLKAR May 24 '12 at 08:09
  • This shows how to write a comma separated list, but what if the desired result is a table with many columns? – UnDiUdin Feb 26 '15 at 13:59
2
declare @city nvarchar(max)
SELECT @city = coalesce(@city+' | ', '')+city FROM cityTable 

EDIT:

In order to show the result

SELECT @city
t-clausen.dk
  • 43,517
  • 12
  • 59
  • 92
1

You can use the PIVOT and UNPIVOT as the example posted on here

TSQL – Transpose Data using Using PIVOT and UNPIVOT

NOTE: You can find the syntax on BOL (books on line) of SQL

Luka Milani
  • 1,541
  • 14
  • 21