Priyanka Kumari

Priyanka Kumari

  • 1.8k
  • 88
  • 7.4k

concatenate multiple rows into one row in sql server

Dec 13 2018 11:41 PM
I have following table
 
PreventMaintainance                   
  ID         Date
  1           2018-12-14
  2           2018-12-16 
 
PreventMaintainanceDetail
ID           Value
1             9874125
1             8745216
1             7251489
 
I want following struture
1           9874125,8745216,7251489
 
I am using following query,where I am getting Incorrect syntax near the keyword 'as'..How I will resolve this??
 
Select
parts=STUFF(
(SELECT ',' + t2.Value
FROM PreventMaintain t1 inner join PreventMaintainanceDetail t2 on  
t1.Id = t2.Id
FOR XML PATH ('')
)
, 1, 1, '') as value,pm.Date
 from dbo.PreventMaintain pm inner join dbo.PreventMaintainanceDetail pd
 on pm.Id=pd.Id

Answers (4)