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 Friday, March 22, 2013 5:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 23, 2013 8:09 AM
Points: 11, 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
Post #1434207
Posted Friday, March 22, 2013 7:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
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 Moden's 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)
Post #1434274
Posted Friday, March 22, 2013 8:03 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 23, 2013 8:09 AM
Points: 11, Visits: 17
Tks. tried that before logging this post but it didn't work ...
Post #1434284
Posted Friday, March 22, 2013 8:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 13,138, Visits: 11,977
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 Moden's 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)
Post #1434288
Posted Friday, March 22, 2013 8:19 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 8, 2014 6:13 AM
Points: 1,694, Visits: 19,550

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



____________________________________________________

How to get the best help on a forum

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

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1434293
Posted Friday, March 22, 2013 8:44 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 23,081, Visits: 31,616
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.



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)
Post #1434310
Posted Friday, March 22, 2013 10:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 23, 2013 8:09 AM
Points: 11, 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
Post #1434354
Posted Friday, March 22, 2013 10:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 23, 2013 8:09 AM
Points: 11, 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
Post #1434360
Posted Friday, March 22, 2013 10:18 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 23,081, Visits: 31,616
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





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)
Post #1434375
Posted Friday, March 22, 2013 10:18 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 1:55 PM
Points: 3,991, Visits: 3,011
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
Post #1434376
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse