2

i have a table with two columns like

VENDOR_NAME   DIRECTORY_NAME
DL_101Records  .ssh
DL_101Records  ADHOC
DL_101Records  ARCHIVED
DL_101Records  INBOUND
DL_101Records  OUTBOUND
DL_101Records  PROCESSED
DL_101Records  pub
DL_101Records  TAKEDOW

N

Is there an easy way to turn this into a single string like

VENDOR_NAME    DIRECTORY_NAME
DL_101Records  .SSH ,ADHOC ,ARCHIVED ,INBOUND ,OUTBOUND ,PROCESSED ,PUB ,TAKEDOWN
Chanukya
  • 5,833
  • 1
  • 22
  • 36
Midhun CM
  • 81
  • 1
  • 8
  • 1
    visit this link http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string – Amit Yadav Feb 17 '17 at 06:18

2 Answers2

1

Try This,

DECLARE @TEMP VARCHAR(MAX)='';
SELECT @TEMP=@TEMP+DIRECTORY_NAME+', ' FROM YOURTABLE
SELECT VENDOR_NAME,@TEMP FROM YOURTABLE
GROUP BY VENDOR_NAME
Thangadurai.B
  • 561
  • 1
  • 3
  • 18
0
SELECT VENDOR_NAME,
       DIRECTORY_NAME = STUFF(
                             (
                                 SELECT ', '+DIRECTORY_NAME AS [text()]
                                 FROM _table AS t
                                 WHERE t.VENDOR_NAME = tt.VENDOR_NAME
                                 ORDER BY t.DIRECTORY_NAME
                                 FOR XML PATH('')
                             ), 1, 2, '')
FROM _table tt
GROUP BY VENDOR_NAME;
anatol
  • 1,680
  • 2
  • 24
  • 47