0

I'm getting two columns from two different tables using sql server 2005.. For that I used the following query..

SELECT [MHD].JobPostingId,
       [C].CandidateName 
  FROM 
       Candidate as C RIGHT JOIN MemberHiringDetails MHD 
                      ON [MHD].MemberId=[C].Id

It returns the following data:

JobPostingId  CandidateName
60            aaa
60            bbb
63            aaa

NULL means, that candidate is erased from that table.. But I need output as follows:

JobPostingId  CandidateName
60            aaa, bbb
63            aaa

How to do this..? If it need stored procedure means, ok.. All I need is solution..

Alexander
  • 3,129
  • 2
  • 19
  • 33
Sakthi
  • 103
  • 2
  • 13

1 Answers1

0

Agreed with Mahmoud Gamal, duplicate. Extracted from another post and changed for your tables:

SELECT C.name,
       STUFF((SELECT ','+ C.CandidateName
               FROM Candidate C
              WHERE C.Id = MHD.MemberId
            FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
 FROM MemberHiringDetails MHD 

thx to OMG Ponies in post: How do I Create a Comma-Separated List using a SQL Query?

Community
  • 1
  • 1
Contisma
  • 71
  • 6