title: Multiple column values into a single column as comma separated value
body: <p>I have a table <code>CommentsTable</code> with columns like, <code>CommentA, CommentB, CommentC, CommentD, CommentE</code>.</p>
  
  <p>All comments columns are <code>VARCHAR (200)</code>, by default all columns are <code>NULL</code> also.</p>
  
  <p>The data looks like:</p>
  
  <pre><code>CommentId   CommentA    CommentB    CommentC    CommentD    CommentE
  ---------------------------------------------------------------------
  12345       NULL        C 001       C 002       NULL        C 003
  45678       C 005       NULL        NULL        C 007       NULL
  67890       C 010       NULL        C 011       C 012       NULL
  36912       C 021       C 023       C 024       C 025       C 026
  </code></pre>
  
  <p>I need to avoid the null values and the remaining values are concatenate with <code>comma</code>.</p>
  
  <p>So, the expected output like:</p>
  
  <pre><code>CommentId    CommetDetails
  -------------------------------
  12345        C 001, C 002, C 003
  45678        C 005, C 007
  67890        C 010, C 011, C 012
  36912        C 021, C 023, C 024, C 025, C 026
  </code></pre>
  
  <p>I tried with simple query:</p>
  
  <pre><code>SELECT CommentId, ISNULL(CommentA, '') + ', ' + ISNULL(CommentB, '') + ', ' + 
         ISNULL(CommentC, '') + ', ' + ISNULL(CommentD, '') + ', ' +
         ISNULL(CommentE, '') [CommentDetails]
  FROM CommentsTable
  WHERE ...... --Some conditions
  </code></pre>
  
  <p>But the unwanted <code>comma</code> are occurred, So added <code>IIF</code></p>
  
  <pre><code>SELECT CommentId, 
         IIF(ISNULL(CommentA, '') &lt;&gt; '', (CommentA + ', '), '') +
         IIF(ISNULL(CommentB, '') &lt;&gt; '', (CommentB + ', '), '') +
         IIF(ISNULL(CommentC, '') &lt;&gt; '', (CommentC + ', '), '') +
         IIF(ISNULL(CommentD, '') &lt;&gt; '', (CommentD + ', '), '') +
         ISNULL(CommentE, '') [CommentDetails]
  FROM CommentsTable
  WHERE ...... --Some conditions
  </code></pre>
  
  <p>But here also, the <code>comma</code> occurred in the last position for some cases (If <code>CommentD, CommetE</code> are <code>NULL</code>.</p>
  
  <p>Is there any way to achieve to solve for all the cases.</p>
  
  <p><a href="http://sqlfiddle.com/#!3/412f0/8" rel="noreferrer"><code>Sample SQL Fiddle</code></a></p>