Selecting a Comma Seperated List in sql

Hello All,

Sometimes we required a comma separated value instead of table. So generally people make User define functions or Cursors or loop but there is a really very simple way to do this in a simple select statement.

See the below query

DECLARE @Users varchar(MAX)
SET @Users = ”
SELECT @Users = @Users + CONVERT(NVARCHAR(50),firstname) + ‘,’
FROM UserDetails
SET @Users = LEFT(@Users,LEN(@Users)-1)
SELECT @Users as users

About Amit Prajapati

I am computer Engg. and I am freelancer. I am working on .net technology and I can developed both web as well as window applications. View all posts by Amit Prajapati

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: