dcsimg

Comma Separated or Slash Separated Row Values in a Column

August 4, 2003



>>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-123456''
A-222222222, ''S-123456''
B-123456873, ''S-121212''
C-125343534, ''S-873645''



TBClientData
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








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers