Reputation: 2378
Result of a query is table,is it possible to write a query that convert this result to a text (for example imagine that result is a table with 4 rows with value 1 to 4 and convert it to 1,2,3,4)
Upvotes: 0
Views: 4076
Reputation: 28423
Try this
DECLARE @result varchar(1000)
SET @result = ''
SELECT @result = @result + StudentId + ',' FROM Student WHERE condition = xyz
select substring(@result, 1, len(@result) -1 --trim extra "," at end
Op like
1,2,3,4,..........
Happy Coding
Upvotes: 1
Reputation: 3498
Try the below query
declare @Var varchar(1000);
set @var = ''
select @Var = @var + CONVERT(varchar, Column1) + ','
from Table1
select @var
Upvotes: 2
Reputation: 43023
Yes, you can do that using FOR XML PATH('')
. For example:
create table test(col varchar(10))
insert into test values ('1'),('2'),('3'),('4')
select STUFF( (select ',' + col
from test
FOR XML PATH('')), 1, 1, '')
Upvotes: 2