SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


concatenate rows using for xml path and new line feed carriage


concatenate rows using for xml path and new line feed carriage

Author
Message
julesbenone
julesbenone
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 17
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)

http://www.flickr.com/photos/14261289@N02/8580154328/


Thanks in adv. for your replies
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26500 Visits: 17557
I can't get to your image but can you just replace the comma with a carriage return?


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



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
julesbenone
julesbenone
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 17
Tks. tried that before logging this post but it didn't work ...
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26500 Visits: 17557
julesbenone (3/22/2013)
Tks. tried that before logging this post but it didn't work ...


What do you mean it didn't work? Remember I don't know what you want for final output. I have a feeling that you are wanting to have everything lined up nice and pretty? That is formatting which is best left to the front end.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Mark Cowne
Mark Cowne
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3163 Visits: 24128
Maybe this?


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



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40324 Visits: 38567
Mark-101232 (3/22/2013)

Maybe this?


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



Yes, this is what I was going to suggest as well.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
julesbenone
julesbenone
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 17
Thanks Sean ... yes its formatting and i do undertsand tsql is not the best approach for doing same but for technical reason i want to do it on the back end / the asp.net gridview that is being used also to populate results has not been helpfull as well
julesbenone
julesbenone
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 17
Tks for your adv Mark but i tried something quite the same without success .. below all options that i used so far


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

------------

2)

Stuff
(

(
Select ',' + City
From #tempCityState
Where State = t.State
For Xml Path(''), Type
).value('.', 'varchar(max)')

,1,1,''
) As Cities

--------------------

3)

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

------------------

4)

Stuff
(

(
Select Char(10) + City
From #tempCityState
Where State = t.State
For Xml Path(''), Type
).value('.', 'varchar(max)')

,1,1,''
) As Cities


Thanks anyway
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40324 Visits: 38567
The following works, but in GRID mode you won't see it and in TEXT mode they won't line up under the Cities column.



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;

with States as (
select distinct
State
from
#tempCityState
)
select
State,
stuff((select '' + 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,0,'') as Cities
from
States;


Drop table #tempCityState




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Ed Wagner
Ed Wagner
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16917 Visits: 10088
Trying to put the presentation in the application instead of in SQL is the best approach. You're never going to get everything all nicely lined up in .NET GridView if it's all in one returned column because your probably using a true-type font in your presentation. I've found it best to return individual fields in your DataTable to the page and then let it handle the presentation.

In your .NET application, you can format the output however you want. In your GridView, instead of using a BoundField, try using a TemplateField and embedding HTML between your child controls. You can probably get it laid out the way you want by using minimal CSS and
tags.

If automated pagination isn't an issue, then another option would be to switch to a Repeater control instead, where you can have a greater level of control over the output.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search