title: How to return an nvarchar(max) in a CLR UDF?
body: <p>Assuming following definition:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Replaces each occurrence of sPattern in sInput with sReplace. This is done 
  /// with the CLR: 
  /// new RegEx(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace). 
  /// The result of the replacement is the return value.
  /// &lt;/summary&gt;
  [SqlFunction(IsDeterministic = true)]
  public static  SqlString FRegexReplace(string sInput, string sPattern, 
        string sReplace)
  {
      return new Regex(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace);
  }
  </code></pre>
  
  <p>Passing in a <code>nvarchar(max)</code> value for <code>sInput</code> with a length > 4000 will result in the value being truncated (i.e. the result of calling this UDF is <code>nvarchar(4000)</code> as opposed to <code>nvarchar(max)</code>.</p>