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.
brettrAsked:
Who is Participating?
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

nList
1,2,5,7

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
0
YZlatCommented:
0
jagssiduralaCommented:
You can get in single variable

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

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

Select       @IntegerList
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

YZlatCommented:
try something like this:
 
CREATE FUNCTION [dbo].[GetAllIDs] ( )
RETURNS VARCHAR(8000)
AS
BEGIN
  DECLARE @RES VARCHAR(8000)
  SELECT @RES = COALESCE(@RES + ',','') + t1.ID
  FROM Table1 as t1
  RETURN (@RES)
END

Open in new window


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

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

Does COALESCE only work for text (varchar) fields?

@jagssidurala:

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

Need a CAST?
0
jagssiduralaCommented:
Chnage the code like below


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

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


Select       @IntegerList
0

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.