Creating comma delimited list from results

Is there a way to have a list of integers output in comma separated format?  This would allow me to than use those results in a WHERE IN ().  The output is only one field that is an integer.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
here it is

with t as (
select 1 n union select 2 union select 5 union select 7
select stuff((select ',' + cast(n as varchar) from t for xml path('')),1,1,'') as nList


first 3 lines is for demo data, in your case

select stuff((select ',' + cast(n as varchar) from myTable for xml path('')),1,1,'') as nList
You can get in single variable

Declare @IntegerList Nvarchar(1024)
Set @IntegerList = ''

Select       @IntegerList = @IntegerList + ColumnName + ','
From      TableName

Select       @IntegerList
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

try something like this:
CREATE FUNCTION [dbo].[GetAllIDs] ( )
  SELECT @RES = COALESCE(@RES + ',','') + t1.ID
  FROM Table1 as t1

Open in new window

and then call it as  SELECT dbo.GetAllIDs()
brettrAuthor Commented:

I'm getting this error:
Conversion failed when converting the varchar value ',' to data type int.

Does COALESCE only work for text (varchar) fields?


Getting same error with your technique.   By the way, will this append an unneeded comma after the last value?

Need a CAST?
Chnage the code like below

Declare @IntegerList Nvarchar(1024)
Set @IntegerList = ''

Select       @IntegerList = COALESCE(@IntegerList + ',','') + Cast(ColumnName as Nvarchar)
From      TableName

Select       @IntegerList

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HainKurtSr. System AnalystCommented:
what happened to my first post? posting again

select stuff((select ',' + cast(n as varchar) from myTable for xml path('')),1,1,'') as nList
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.