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 2008 Forums
 Transact-SQL (2008)
 separate data with comma

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-19 : 09:42:09
Hi,
In sql, how can I separate values in a field i.e. field1 by comma?
i.e.:

field1, field2
"hello" "james"
"myname" "get there"
"where" "moon"

I would like to retrieve:
hello, myname, where
How is this done please without using a cursor?
Thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-19 : 09:48:10
see
http://www.nigelrivett.net/SQLTsql/CSVStringSQL.html

select csvstr = stuff (
(
select ',' + field1
from tbl
for xml path('')
)
,1,1,'')



==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

lionofdezert
Aged Yak Warrior

885 Posts

Posted - 2012-07-19 : 09:52:28
SELECT SUBSTRING(( SELECT ', ' + columnName
FROM YourTAble
FOR
XML PATH('')), 2, 8000) AS CSV

--------------------------
http://connectsql.blogspot.com/
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-07-19 : 10:17:55
Thanks
Go to Top of Page
   

- Advertisement -