Search This Blog & Web

Friday, May 22, 2009

Returning Comma seperated Values in SQL Server 2005

Declare @vComma Varchar(max);

Select @vComma = COALESCE(@vComma+';','') + column1 from table1

select @vComma

I have 5 values in Unique table against Val column and this command will return all values as ; seperated


3 comments:

Shamas DBA said...

Yes this works and is good but their must be some other ways because I saw people using function some where and XML also.
Can any one have example for this.

Shamas DBA said...

Select column +’,’ as [Text()] from tablename for xml path(‘’)

is another way around to return xml column with , seperation

Anonymous said...

Select cast(intregistrationid as varchar(10) ) +','
from te_registration
where intstatus = 174 and intseasonid = 1
for xml path('')


[text()] causes problems as column alias. because it creates xml tag with this name.