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

  • SEARCH
  • Home
  • 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 7880275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:06:22+00:00 2026-06-03T04:06:22+00:00

I have the following DateFormat dformat = new SimpleDateFormat(yyyy-M-d); dformat.setLenient(false); Date cin = dformat.parse(cinDate);

  • 0

I have the following

DateFormat dformat = new SimpleDateFormat("yyyy-M-d");
            dformat.setLenient(false);
            Date cin = dformat.parse(cinDate);

and the sql function

create or replace function search(_checkIn date, _checkOut date) returns setof Bookings as $$
declare
    r Bookings;
begin
    for r in
    select * from Bookings
    loop
        if ((_checkIn between r.checkIn and r.checkOut) or (_checkOut between r.checkIn and r.checkOut)) then
            return next r;
        end if;
    end loop;
    return;
end;
$$ language plpgsql;

The date format for the postgresql is standard (default)

create table Bookings (
    id          serial,
    status      bookingStatus not null,
    pricePaid   money not null,
    firstName   text,
    lastName    text,
    address     text,
    creditCard  text,
    checkOut    date not null,
    checkIn     date not null,
    room        integer not null,
    extraBed    boolean not null default false,

    foreign key (room) references Rooms(id),
    primary key (id)
);

and I’m trying to parse a date into the function so it can return a table for me, I seem to run into the issue of date formatting (which is why I think I’m getting this error)

org.postgresql.util.PSQLException: ERROR: syntax error at or near "Feb"

So I was wondering how would I fix this problem, I don’t know how to format the date properly

EDIT:

I’m calling the query like this

           try {
                String searchQuery = "SELECT * FROM Rooms r where r.id not in (select * from search(" + cin +", " + cout +"))";
                PreparedStatement ps = conn.prepareStatement(searchQuery);
                rs = ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }

so I think the error comes in because the way I format the date is wrong and postgres won’t read it

  • 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-06-03T04:06:23+00:00Added an answer on June 3, 2026 at 4:06 am

    It sounds like you’re passing the argument by concatenating them directly into the string. This is a very bad idea, since it can lead to SQL injections. Always use PreparedStatements with the ? place-holders to pass parameters, never pass them directly by concatening them directly into the query string (more so, you’d need the ' delimiters around).

    You could have something like:

     PreparedStatement stmt
         = con.prepareStatement("SELECT id FROM Bookings WHERE checkIn=?")
     stmt.setDate(1, new java.sql.Date(cin.getTime()));
          // ? parameters are indexed from 1
     ResultSet results = stmt.executeQuery();
    

    Alternatively, PostgreSQL internal date conversion is usually fairly good and flexible. You could cast the string parameter to a date with PostgreSQL:

     PreparedStatement stmt
         = con.prepareStatement("SELECT id FROM Bookings WHERE checkIn=CAST(? AS DATE)");
     stmt.setString(1, cinDate);
     ResultSet results = stmt.executeQuery();
    

    This is flexible, but might not lead to the exact result you need depending on the date format (you can check the PostgreSQL manual for details on date conversion formats). The input format you’re using should work just fine, though (Try SELECT CAST('2012-05-01' AS DATE) directly in PostgreSQL, for example, this will return a correct PostgreSQL date.)

    Note that when using new java.sql.Date(cin.getTime()), you’re likely to run into time zone issues. You could use java.sql.Date.valueOf(...) too.

    To clarify, following your edit:

    This will not work, since the dates would be part of the SQL syntax itself, not strings or dates: "SELECT * FROM Rooms r where r.id not in (select * from search(" + cin +", " + cout +"))"

    You’d at least need to use ' quotes: "SELECT * FROM Rooms r where r.id not in (select * from search("' + cin +"', '" + cout +"'))". Here, to a degree, you could expect the parameters to be formatted properly, but don’t do it. In addition, would would still have to cast the string using CAST('...' AS DATE) or '...'::DATE.

    The simplest way would certainly be:

    String searchQuery = "SELECT * FROM Rooms r where r.id not in (select SOMETHING from search(CAST(? AS DATE), CAST(? AS DATE)))";
    PreparedStatement ps = conn.prepareStatement(searchQuery);
    ps.setString(1, cinDate);
    ps.setString(2, coutDate);
    

    (As a_horse_with_no_name pointed out in a comment, the general query wouldn’t work anyway because of your inner select.)

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

Sidebar

Related Questions

I have the following scenario : SimpleDateFormat dateFormat = new SimpleDateFormat(dd/MM/yyyy); System.out.println(dateFormat.parse(31/05/2011)); gives an
I have the following code: SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy-dd-MM HH:mm:ss); Date convertedDate; String
I have the following Java: DateFormat formatter = new SimpleDateFormat( EEE MMM dd yyyy
I have the following code: SimpleDateFormat dateFormat = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); TimeZone tz2 =
The following test fails: DateFormat df = new SimpleDateFormat(HH:mm:ss z); assertEquals(00:00:00 GMT, df.format(new Date(0)));
I have a string that contains a date, in the following format: dd-mm-yyyy with
I have the following code: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @yyyy-mm-dd;
I have the following string i need to parse to a date: 2012-01-31T08:41:12.5462495+01:00 now
I have the following date format: 2010-04-15 23:59:59 How would I go about converting
I have the following date format: Tue Mar 06 17:45:35 -0600 2012 and I

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.