title: How to strip comments starting at ** line form text in sql
body: <p>Postgres 8.3+  database text column contains possible multi line expression.</p>
  
  <p>After expression there and be empty lines and comment area.
  Comment  area starts with ** characters at start of line.
  There can be only empty lines in end of expression</p>
  
  <p>Examples</p>
  
  <pre><code>1+
  2
  ** this
  is comment
  </code></pre>
  
  <p>With trailine extra newline:</p>
  
  <pre><code>1+
  2
  
  ** this
  is comment and newline after expression
  </code></pre>
  
  <p>How to strip empty lines and comments from expression?
  Empty lines inside expression shoudl replaced to spaces. Empty lines and comments at end of expression should stripped. Result should be <code>1+ 2</code> in both cases.
  If there is only comment, empty string should returned.</p>
  
  <p>I tried method below but this does not replace newlines inside expression with spaces. How to fix ?</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION public.RemoveComment(value text) RETURNS text AS
  $BODY$
  select case when tulem like '**%' then ''
    else tulem end
  from (  
  SELECT trim(lower( Translate(   regexp_replace($1, E'\n\\*\\*.*', '', 'g'),E'\n\r', '')    )) as tulem
  ) tulem;
  
  $BODY$ language sql immutable;
  </code></pre>