Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««123

concatenate rows using for xml path and new line feed carriage Expand / Collapse
Author
Message
Posted Saturday, March 23, 2013 7:14 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 23, 2013 8:09 AM
Points: 11, Visits: 17
based on your various recommendations i finally found a good workable solution without having to compromise that much with my early tsql code in the views. i stayed with my first tqsl as follow:

Stuff
(
(
Select ',' + City As [text()]
From #tempCityState
Where State = t.State
For Xml Path('')
)
, 1, 1, ''
) As Cities,

Please note this is part of a view that is used in a stored procedure that feeds an asp.net gridview

i made this adjustment in my stored procedures:

Replace(ViewName.Cities, ',' , '
') AS Cities

then after i added the HtmlEncode="False" property recommended by Sean in the BoundField.

by using this solution i didn't have to review rows concactenation tsql code(s) i have in several views and as a result affect others sto proc and frontends that connect to my sql server database.

To conclude i resolved my formatting problem by adding one additional property in my gridview boundfield and slightly adjusted a specific stored procedure that is only used by the asp.net application

Thanks to all who have contributed in this thread ... i think the topic can be closed now .. happy week end


Post #1434606
Posted Monday, June 30, 2014 1:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 30, 2014 1:09 AM
Points: 1, Visits: 0
This code is a modification to the above post to have the cities adjusted under each other

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';
with States as (
select distinct
State
from
#tempCityState
)
select
State,
stuff((select CHAR(9) + ' ' + City + char(13) + char(10)
from #tempCityState tcs
where tcs.State = States.State
order by tcs.City
for xml path(''),TYPE).value('.','varchar(max)'), 1, 3, '') as Cities
from
States;
Drop table #tempCityState
Post #1587516
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse