Click here to Skip to main content
15,891,895 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have something like this:

demo table
Id name
1 abcd.
2 efgh.
3 ijkl.
4 mnop.

What I'd like is to query back:

abcd.efgh.ijkl.mnop.

I need something to get all the rows at once..

thanks in advance..
Posted

DECLARE @Result as Varchar(Max)
SELECT @Result =COALESCE(@Result,'') + (IsNull(x.Name,'')) + '.' from
(
SELECT Name
FROM Demo
)x
Select @Result as 'Result'

If you want this then click accept solution
 
Share this answer
 
Here is a solution using STUFF:

SQL
DECLARE @Heroes TABLE (
    [Heroes_ID] int,
    [Heroes_Name] NVARCHAR(20)
)

INSERT INTO @Heroes ( [Heroes_ID], [Heroes_Name] )
VALUES ( 1,'Superman' ), ( 2,'Batman' ), (3,'Ironman'), (4,'Wolverine')

SELECT * FROM @Heroes

SELECT STUFF((SELECT ', ' + [Heroes_Name]
			  FROM @Heroes
			  ORDER BY [Heroes_Name]
			  FOR XML PATH('')), 1, 1, '') AS [Output]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900