Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Putting commas between select statement values

Author  Topic 

Spence
Starting Member

2 Posts

Posted - 2005-01-12 : 07:44:45
Hello,

This may be a strange request, but I am going to ask about it anyways.

Say for example if I have a table named TEST and in the table there is a column named NUMBERS, such that it is like this:

NUMBERS
1
2
3
4

How could I use a select statement in a way that a comma would seperate every return value, such that if I go 'Select NUMBERS from TEST' I would get:

1,2,3,4

Instead of:

1
2
3
4

Any ideas?

Thanks

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-01-12 : 07:53:23
Try this

Declare @s nvarchar(300)
set @s=''
select @s=@s+','+convert(nvarchar(20),numbers) from test
select substring(@s,2,len(@s))

Madhivanan
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-12 : 10:45:57
or

Declare @s nvarchar(300)
select @s = coalesce(@s + ',', '') + convert(nvarchar(20),numbers) from test
select @s


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-01-12 : 10:50:59
Man...ain't this a popular one

http://weblogs.sqlteam.com/brettk/archive/2005/01/05/3946.aspx



Brett

8-)
Go to Top of Page
   

- Advertisement -