title: R scripts in SQL Server 2016 corrupted with Â character
body: <p>I have found a strange behaviour of SQL Server 2016 when handling broken pipes inside R scripts. See the T-SQL code below:</p>
  
  <pre><code>DECLARE 
      @r nvarchar(100)
  
  /* Create a data frame with a broken pipe as one of its fields and a simple ASCII encoded string in another. */
  SET @r = N'
  df &lt;- data.frame(
      a = "¦",
      b = "a,b,c"
      )';
  
  /* Print @r to detect the inclusion of any unwanted characters. */
  PRINT @r;
  
  /* Execute and retrieve the output. */
  EXECUTE sp_execute_external_script
      @language = N'R',
      @script = @r,
      @output_data_1_name = N'df'
  WITH RESULT SETS ((
      BadEncodingColumn varchar(2),
      GoodEncodingColumn varchar(5)
      ));
  </code></pre>
  
  <p>The PRINT command returns this in the Messages tab:</p>
  
  <pre><code>df &lt;- data.frame(
      a = "¦",
      b = "a,b,c"
      )
  </code></pre>
  
  <p>However, the final Results tab looks like this:</p>
  
  <pre><code>BadEncodingColumn   GoodEncodingColumn
  Â¦                  a,b,c
  </code></pre>
  
  <p>This behaviour seems to emerge at the EXECUTE sp_execute_external_script phase of the script, and I have seen this character (Â) when dealing with other encoding issues with Excel, R and other versions of SQL Server.</p>
  
  <p>Any solutions to this behaviour? And bonus points, what is 'special' about the Â character?</p>
  
  <p>Edit: I have tried tinkering with data types inside SQL Server and R to no avail. </p>