SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 concatenation of rows
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

guddu_12
Starting Member

United Kingdom
1 Posts

Posted - 02/04/2013 :  06:03:41  Show Profile  Reply with Quote
Hi ,

I have a situation where i have to concatenate rows based of group of value or without group, i don't want to use tsql. can any one let me know wether we have any function for it.

eg
train_no direction
123 south-east
124 south-east
125 south-east
126 south-east
131 south-west
132 south-west
156 east

I want result as below
train,no direction
(123,124,125,126) South-East
(131,132) South-West
(156) esst

Please help

James K
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 02/04/2013 :  06:11:07  Show Profile  Reply with Quote
SELECT
	STUFF(trains,1,1,'') AS trains,
	direction
FROM
	(SELECT DISTINCT direction FROM TheTable) a
	CROSS APPLY
	(
		SELECT ',' + CAST( train_no AS varchar(32))
		FROM TheTable b 
		WHERE b.direction = a.direction
		FOR XML PATH('')
	) c(trains)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000