Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:29:23+00:00 2026-05-31T15:29:23+00:00

I am trying to create an SQL select statement in PL/pgSQL procedure. It was

  • 0

I am trying to create an SQL select statement in PL/pgSQL procedure. It was all fine until I had to add a date to the select. The problem is that to compare dates in select clause my date must be enclosed in single quotes '' (e.g. ‘01.01.2011’), but in my case it is already a text type and I can’t add it there.

Below is a sample code that should have same problem:

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>' || 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;

Is there any other way to “escape” date? Or some other suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:29:24+00:00Added an answer on May 31, 2026 at 3:29 pm

    To use insert values safely and efficiently into a dynamically build and executed SQL string, there are a number of possibilities. The best one is to use the USING clause like this:

    CREATE OR REPLACE FUNCTION sample(_date timestamp without time zone)
    RETURNS integer AS
    $BODY$
    BEGIN
        RETURN QUERY EXECUTE '
        SELECT count(*)::int
        FROM   test t
        WHERE  t.created > $1'
        USING  _date;
    END;
    $BODY$ LANGUAGE plpgsql VOLATILE;
    

    This is actually a bad example, because it could be simplified to:

    CREATE OR REPLACE FUNCTION sample(_date timestamp)
    RETURNS integer AS
    $BODY$
        SELECT count(*)::int
        FROM   test
        WHERE  created > $1;
    $BODY$ LANGUAGE sql;
    

    Another way would be to use quote_literal().
    I wrote more about dynamic SQL in plpgsql here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perplexing SQL select statement (ugly) that I'm trying to write in
I'm trying to create sql statement in TSQL that looks like this: INSERT INTO
I am trying to write a SQL statement that will create a flattened table
I am trying to create SQL Select that returns counts of a certain field
I'm trying to write a simple winforms application that executes a SQL SELECT statement
I am trying to create a SQL statement that will allow me to remove
I am trying to create an SQL statement that will essentially calculate points due
I'm trying to create an SQL statement that joins two tables and returns a
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm trying to create a LINQ to SQL class that represents the latest version

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.