Comma Separated or Slash Separated Row Values in a Column | Database Journal

Comma Separated or Slash Separated Row Values in a Column

Aug 4, 2003
1 minute read

>>Script Language and Platform: SQL Server 2000
Converts the grouped row values separated by commas or slash.
If you have a table such as the one below:

TBFundInOut
IDNO AccountNo
A-123456789, ”S-123456A-222222222, ”S-123456B-123456873, ”S-121212C-125343534, ”S-873645TBClientData
IDNO Name
A-123456789, ”Peter”
A-222222222, ”Chan”
B-123456873, ”Wong”
C-125343534, ”LEE”


query result will be:

AccountNo Name
”S-123456”, Peter/Chan
”S-121212”, Wong
”S-873645”, LEE

Usage:

select distinct TBF.AccountNo, dbo.slash(AccountNo) as Name
from TBFundInOut as TBF
inner join TBClientData as TBC
on TBF.IDNO = TBC.IDNO


or

select distinct TBF.AccountNo, dbo.Comma(AccountNo) as Name
from TBFundInOut as TBF
inner join TBClientData as TBC
on TBF.IDNO = TBC.IDNO

Note: Adjust the varchar length according to your requirement.

Author: MAK

create function dbo.slash (@AccountNo varchar(20) ) returns varchar(100)
as
begin
declare @x varchar(100)
set @x =””
select @x = @x +/+Name from TBClientData where IDNO in
(select IDNO from TBFundInOut where AccountNo= @AccountNo)
–print right(@x,len(@x)-1)
set @x = right(@x,len(@x)-1)
return (@x)
end
GO
create function dbo.Comma (@AccountNo varchar(20) ) returns varchar(100)
as
begin
declare @x varchar(100)
set @x =””
select @x = @x + ”,” +Name from TBClientData where IDNO in
(select IDNO from TBFundInOut where AccountNo= @AccountNo)
–print right(@x,len(@x)-1)
set @x = right(@x,len(@x)-1)
return (@x)
end



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.