title: PostgreSQL execute + date
body: <p>I am trying to create an SQL select statement in PL/pgSQL procedure. It was all fine until I had to add a <code>date</code> to the select. The problem is that to compare dates in select clause my date must be enclosed in single quotes <code>''</code> (e.g. '01.01.2011'), but in my case it is already a <code>text</code> type and I can't add it there.</p>
  
  <p>Below is a sample code that should have same problem:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION sample(i_date timestamp without time zone)
    RETURNS integer AS
  $BODY$
  DECLARE
    _count integer := 0;
    _sql text := '';
  BEGIN
  
    IF i_date IS NOT NULL THEN
     _cond := _cond || ' AND t.created&gt;' || i_date;
  END IF;
  
  _sql := 'SELECT count(*) FROM test t WHERE 1=1' || _cond;
  
  EXECUTE _sql INTO _count;
  RETURN _count;
  
  END;
  $BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
  </code></pre>
  
  <p>Is there any other way to "escape" date? Or some other suggestions?</p>