Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
persiangulf098
Starting Member
10 Posts |
Posted - 2010-02-18 : 13:23:53
|
| HELLO1 2 -- --1 1004 501 2006 201 455 1205 1206 80I want this result1,100,200,454,506,20,805,120,80 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-18 : 13:27:04
|
| [code]SELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + b.[2] FROM <urtable> AS b WHERE b.[1] = a.[1] ORDER BY ',' + b.[2] FOR XML PATH('')), 1, 1, '') AS [csv]FROM <urtable> AS aORDER BY a.[1][/code]EDIT : [1] and [2] are ur column names...from the sample. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
persiangulf098
Starting Member
10 Posts |
Posted - 2010-02-18 : 15:31:12
|
quote: Originally posted by vijayisonly
SELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + b.[2] FROM <urtable> AS b WHERE b.[1] = a.[1] ORDER BY ',' + b.[2] FOR XML PATH('')), 1, 1, '') AS [csv]FROM <urtable> AS aORDER BY a.[1]EDIT : [1] and [2] are ur column names...from the sample.
Thanks for your attention butThis error :Conversion failed when converting the varchar value ',' to data type int. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-18 : 15:35:13
|
convert ur column [2] to a varchar value before concatenation... likeSELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + convert(varchar(10),b.[2]) FROM table AS b WHERE b.[1] = a.[1] ORDER BY ',' + convert(varchar(10),b.[2]) FOR XML PATH('')), 1, 1, '') AS [csv]FROM table AS aORDER BY a.[1] |
 |
|
|
persiangulf098
Starting Member
10 Posts |
Posted - 2010-02-18 : 15:49:49
|
quote: Originally posted by vijayisonly convert ur column [2] to a varchar value before concatenation... likeSELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + convert(varchar(10),b.[2]) FROM table AS b WHERE b.[1] = a.[1] ORDER BY ',' + convert(varchar(10),b.[2]) FOR XML PATH('')), 1, 1, '') AS [csv]FROM table AS aORDER BY a.[1]
Sorry ,I have result set but csv has null values |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-18 : 15:53:19
|
What does that mean? I get correct results with your sample data.declare @t table ([1] int, [2] int)insert @tselect 1, 100union all select 4, 50union all select 1, 200union all select 6, 20union all select 1, 45union all select 5, 120union all select 5, 120union all select 6, 80 SELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + convert(varchar(10),b.[2]) FROM @t AS b WHERE b.[1] = a.[1] ORDER BY ',' + convert(varchar(10),b.[2]) FOR XML PATH('')), 1, 1, '') AS [csv]FROM @t AS aORDER BY a.[1] |
 |
|
|
persiangulf098
Starting Member
10 Posts |
Posted - 2010-02-18 : 16:21:34
|
quote: Originally posted by vijayisonly What does that mean? I get correct results with your sample data.declare @t table ([1] int, [2] int)insert @tselect 1, 100union all select 4, 50union all select 1, 200union all select 6, 20union all select 1, 45union all select 5, 120union all select 5, 120union all select 6, 80 SELECT DISTINCT a.[1], STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + convert(varchar(10),b.[2]) FROM @t AS b WHERE b.[1] = a.[1] ORDER BY ',' + convert(varchar(10),b.[2]) FOR XML PATH('')), 1, 1, '') AS [csv]FROM @t AS aORDER BY a.[1]
Thanks |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-02-18 : 18:03:15
|
You're welcome. |
 |
|
|
|
|
|
|
|