Statistics

Total Posts: 34
This Year: 0
This Month: 0
This Week: 0
Comments: 174


RSS 2.0   SocialTwist Tell-a-Friend


Admin

Sign In

Navigation


Recent Posts


On this page....

Converting Row Data as Comma Separated String

Archives

 Full Archives By Category
 2007 Calendar View
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Categories

CDOSYS (1) Classic ASP (10) Command Line (2) Databases (16) Excel (1) HTML (1) IIS (10) Indexing Service (1) Internet Explorer (7) Media Streaming (1) MS.Net (2) SQA (7) SQL Server (16) Windows OS (2)

Blogroll - Fav Blogs


Acknowledgments

DasBlog Theme Design by: Tom Watts
E-mail: Send mail to the author(s)
Theme Image by: dreamLogic

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Technology Blog

# Saturday, December 20, 2008
Author: Akash Heranjal

One of my officemate Ranjan did some research on internet and found a simple and quick solution to have row data as comma separated string. The requirement of the task is as follows

Example Requirement Case:

There is a table by name tbl_CityState with three columns, RecId which is an Identity Column, StateName which is VarChar(50) holding the name of the state or province, and finally the CityName which is VarChar(50) holding the name of the cities with-in a State or Province. The script for creating the table is as below

CREATE TABLE [dbo].[tbl_CityState](
[RecID] [int] IDENTITY(1,1) NOT NULL,
[StateName] [varchar](50) NOT NULL,
[CityName] [varchar](50) NOT NULL,
CONSTRAINT [PK_tbl_CityState] PRIMARY KEY CLUSTERED
(
[RecID] ASC
) ON [PRIMARY]
) ON [PRIMARY]


To Insert data in the table, run the below scripts

Insert into tbl_CityState (StateName, CityName) Values('Karnataka', 'Bangalore')
Insert into tbl_CityState (StateName, CityName) Values('Karnataka', 'Mangalore')
Insert into tbl_CityState (StateName, CityName) Values('Karnataka', 'Hubli')
Insert into tbl_CityState (StateName, CityName) Values('Karnataka', 'Mysore')
Insert into tbl_CityState (StateName, CityName) Values('Goa', 'Madgaon')
Insert into tbl_CityState (StateName, CityName) Values('Goa', 'Panaji')
Insert into tbl_CityState (StateName, CityName) Values('Maharastra', 'Mumbai')
Insert into tbl_CityState (StateName, CityName) Values('Maharastra', 'Pune')


The table looks like this…


The task is to get the name of the cities separated by comma against each distinct state


Solution:

Create a User Defined Function, for example func_CityName

CREATE FUNCTION [dbo].[func_CityName]
(
@StateName VarChar(100)
)
RETURNS Varchar(7000)
AS
BEGIN

DECLARE @result AS VarChar(4000)
SELECT @result = (coalesce(@result + ', ' , '' ) + B.CityName)
FROM tbl_CityState AS B WHERE B.StateName=@StateName

RETURN @result

END


The function above takes the name of the state as input, creates a string of all cities where the state name is as that of the input parameter and finally returns the concatenated comma separated string of city names. The key logic is the effective usage of COALESCE to build string.

SELECT @result = (COALESCE(@result + ', ' , '') + B.CityName)
FROM tbl_CityState AS B WHERE B.StateName=@StateName