Author |
Topic |
Mauricio Moreno
Starting Member
18 Posts |
Posted - 2008-02-07 : 19:44:36
|
Hello,I have a delima, and im not really sure if this possible. But i have a table like lets sayid | data11 this2 that3 stuffi want to be able to return this as one row with the data from data1 in one column seperated by commas.so the result would be1 Columnthis, that, stuffcan anyone help me with this.Thank you,~ Moe |
|
CShaw
Yak Posting Veteran
65 Posts |
Posted - 2008-02-07 : 20:52:47
|
You could do it in a Cursor, but I bet there are better answers. You may want to post this in the T-SQL Forum.Chris Shawwww.SQLonCall.com |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-08 : 00:19:31
|
You want entire column values of table in one row? then use this,In SQL 2005SELECT LEFT(l.list,LEN(l.list)-1)FROM(SELECT data1 + ',' AS [text()]FROM TableFOR XML PATH(''))l(list) If SQL 2000:-DECLARE @list varchar(8000)SELECT @list=CusName + ',' + COALESCE(@list,'')FROM tSELECT LEFT(@list,LEN(@list)-1) |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-08 : 01:24:34
|
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspxMadhivananFailing to plan is Planning to fail |
 |
|
Arshiya
Starting Member
1 Post |
Posted - 2008-06-03 : 02:43:12
|
this query is working fine but the problem is how do i group this for eg:i have a View an ID SubID 1234 112 1234 113 1234 114I want it to be displayed as ID SubID1234 112,113,114Plz help me with this quote: Originally posted by visakh16 You want entire column values of table in one row? then use this,In SQL 2005SELECT LEFT(l.list,LEN(l.list)-1)FROM(SELECT data1 + ',' AS [text()]FROM TableFOR XML PATH(''))l(list) If SQL 2000:-DECLARE @list varchar(8000)SELECT @list=CusName + ',' + COALESCE(@list,'')FROM tSELECT LEFT(@list,LEN(@list)-1)
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-06-03 : 03:31:32
|
[code]DECLARE @Sample TABLE (ID INT, SubID INT)INSERT @SampleSELECT 1234, 112 UNION ALLSELECT 1234, 113 UNION ALLSELECT 1234, 114 UNION ALLSELECT 3234, 212 UNION ALLSELECT 3234, 213 UNION ALLSELECT 3234, 214SELECT i.ID, STUFF(g.y, 1, 1, '') AS SubIDsFROM ( SELECT ID FROM @Sample GROUP BY ID ) AS iCROSS APPLY ( SELECT DISTINCT ',' + CAST(SubID AS VARCHAR(11)) FROM @Sample AS s WHERE s.ID = i.ID ORDER BY ',' + CAST(SubID AS VARCHAR(11)) FOR XML PATH('') ) AS g(y)[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
musthaan
Starting Member
1 Post |
Posted - 2012-03-02 : 05:50:40
|
I have Two Options,1)``````declare @x varchar(100)select @x= isnull(@x+', ','')+CityName from lkpUAECityselect @x2)```````SELECT LEFT(l.list,LEN(l.list)-1)FROM(SELECT CityName + ',' AS [text()]FROM lkpUAECityFOR XML PATH(''))l(list)Musthaan Majeed |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-02 : 12:25:21
|
quote: Originally posted by musthaan I have Two Options,1)``````declare @x varchar(100)select @x= isnull(@x+', ','')+CityName from lkpUAECityselect @x2)```````SELECT LEFT(l.list,LEN(l.list)-1)FROM(SELECT CityName + ',' AS [text()]FROM lkpUAECityFOR XML PATH(''))l(list)Musthaan Majeed
what was the purpose of opening an old thread?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|