title: Concatenating distinct column values in SQL Server
body: <p>I am trying to concatenate many columns and separating it with a comma as below:</p>
  
  <pre><code>Column
  ------
  abc
  bcd
  bgd
  abc
  </code></pre>
  
  <p>Expected output: <code>abc,bcd,bgd</code></p>
  
  <p>I am using this code:</p>
  
  <pre><code>CREATE FUNCTION concatinate(@PK uniqueidentifier)
  RETURNS varchar(max)
  AS 
  BEGIN
      DECLARE @result varchar(max)
  
      SELECT @result = ISNULL(@result + ', ', '') + Column
      FROM table
  </code></pre>
  
  <p>The result I am getting is</p>
  
  <pre><code>abc,bcd,bgd,abc
  </code></pre>
  
  <p>I am not able to only select the distinct values. Please guide.</p>