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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to join rows

Author  Topic 

lols
Posting Yak Master

174 Posts

Posted - 2008-06-26 : 06:32:35
Hi,

I have the following:

State
-----
OA
CA
TX
AR
TU
IN

I want the result as
States
-------------------
OA,CA,TX,AR,TU,IN

How to do this?

raky
Aged Yak Warrior

767 Posts

Posted - 2008-06-26 : 06:45:03
quote:
Originally posted by lols

Hi,

I have the following:

State
-----
OA
CA
TX
AR
TU
IN

I want the result as
States
-------------------
OA,CA,TX,AR,TU,IN

How to do this?



Try this

declare @t table ( state varchar(50))
insert into @t
select 'OA' union all
select 'CA' Union all
select 'TX' Union all
select 'AR' Union all
select 'TU' Union all
select 'IN'

select distinct stuff( (select ','+state from @t for xml path('')),1,1,'') as states from @t
Go to Top of Page

lols
Posting Yak Master

174 Posts

Posted - 2008-06-26 : 06:51:48
thanks..very helpful
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-06-26 : 09:48:14
also see
http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -