title: Counting number of weekdays between two dates
body: <p>I am trying to get the number of weekdays between two dates in plpgsql. The following is my code:</p>
  
  <pre><code>CREATE FUNCTION weekdays(DATE, DATE) RETURNS INTEGER AS
  $$
  DECLARE
      d date := $1;
      weekdays integer := 0
  BEGIN
  
  LOOP
      IF select extract(dow from date d) != 6 or select extract(dow from date d) != 0
        weekdays := weekdays +1
      END IF
      d := d + 1;
      EXIT WHEN d &gt; $2;
  END LOOP;
  RETURN weekdays;
  END;
  $$
  LANGUAGE 'plpgsql' IMMUTABLE;
  </code></pre>
  
  <p>However, I keep getting the following error:</p>
  
  <blockquote>
  <pre><code>Unterminated dollar quote started at position 56 in SQL
  CREATE FUNCTION weekdays(DATE, DATE) RETURNS INTEGER AS $$ DECLARE d date := $1.
  Expected terminating $$
  </code></pre>
  </blockquote>