Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted Aug 4, 2003

Comma Separated or Slash Separated Row Values in a Column

By DatabaseJournal.com Staff



>>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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date