Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

concatenate rows using for xml path and new line feed carriage

$
0
0
Good day Guys

i have this code

--------

Create TABLE #tempCityState (State Varchar(5), City Varchar(50))

Insert Into #tempCityState
Select 'CO', 'Denver' Union
Select 'CO', 'Teluride' Union
Select 'CO', 'Vail' Union
Select 'CO', 'Aspen' Union
Select 'CA', 'Los Anggeles' Union
Select 'CA', 'Hanford' Union
Select 'CA', 'Fremont' Union
Select 'AK', 'Wynne' Union
Select 'AK', 'Nashville'


Select Distinct State, (Select Stuff((Select ',' + City
From #tempCityState
Where State = t.State
FOR Xml Path('')),1,1,'')) AS Cities
From #tempCityState t

-- or tentatively

--Select Distinct State, (Select Substring((Select ',' + City
-- From #tempCityState
-- Where State = t.State
-- FOR Xml Path('')),2,200000)) AS Cities
--From #tempCityState t


Drop table #tempCityState

-----------

it gives this results:

State City
------------------
AK Nashville,Wynne
CA Fremont,Hanford,Los Anggeles
CO Aspen,Denver,Teluride,Vail


but i want output like this (3 rows exactly with new line feed carriage after each item)

3 | Flickr - Photo Sharing!


Thanks in adv. for your replies

Viewing all articles
Browse latest Browse all 13329

Trending Articles