title: How to concatenate many SQL rows into single text string for int
body: <p>The SQL table I have is as follows:</p>
  
  <pre><code>ID     Name   NameID 
  1      John   1
  1      Dan    2
  1      Jill   3
  2      Jack   4
  2      Sam    5
  </code></pre>
  
  <p>I need this table to be like this:</p>
  
  <pre><code>ID      Name             NameID 
  1       John, Dan, Jill  1, 2, 3
  2       Jack, Sam        4, 5
  </code></pre>
  
  <p>The SQL query that I have for now is as follows:</p>
  
  <pre><code>SELECT ID, 
      STUFF((
          SELECT N', ' + Name 
          FROM Table2 b 
          WHERE b.NameID = a.NameID 
          FOR XML PATH (''),TYPE).value('text()[1]','nvarchar(max)'
          ),1,2,N'') AS Name,
      NameID
  FROM Table1 a
  </code></pre>
  
  <p>That query does what I need for the Name column but it won't work when I try to do it for the <code>NameID</code> column. I need all the cells that have <code>int</code> to remain as <code>int</code>. I cannot have them as <code>string</code> since I will be needing to filter the table by the <code>NameID</code>. How do I get all <code>NameID</code> into a single cell for each <code>ID</code>?</p>