Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 21D54B5FB9D for ; Tue, 31 May 2011 00:12:51 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 72823-10 for ; Tue, 31 May 2011 03:12:43 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ey0-f174.google.com (mail-ey0-f174.google.com [209.85.215.174]) by mail.postgresql.org (Postfix) with ESMTP id 62EAFB5F712 for ; Tue, 31 May 2011 00:12:43 -0300 (ADT) Received: by eyx24 with SMTP id 24so1515464eyx.19 for ; Mon, 30 May 2011 20:12:42 -0700 (PDT) Received: by 10.14.9.72 with SMTP id 48mr2021403ees.113.1306811562279; Mon, 30 May 2011 20:12:42 -0700 (PDT) MIME-Version: 1.0 Received: by 10.14.47.131 with HTTP; Mon, 30 May 2011 20:12:12 -0700 (PDT) X-Originating-IP: [1.144.179.228] In-Reply-To: <14646.1306806733@sss.pgh.pa.us> References: <14646.1306806733@sss.pgh.pa.us> From: Simon Mitchell Date: Tue, 31 May 2011 13:12:12 +1000 Message-ID: Subject: Re: PreparedStatement error for date parameter with overlaps To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=0016364c7efff541e404a489c6e8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-10 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201105/83 X-Sequence-Number: 21913 --0016364c7efff541e404a489c6e8 Content-Type: text/plain; charset=ISO-8859-1 This is a very minor issue, the problem only occurs with the prepared statement, a query string without prepared works fine. I was just following it up as it was a problem posted on an Australian forum, the original issue has been resolved. http://forums.whirlpool.net.au/forum-replies.cfm?t=1708081&p=-1&#bottom Anyway, so the sql I am using is just based on the overlaps example from the postgresql doc http://www.postgresql.org/docs/9.0/static/functions-datetime.html The strange thing is the doing a toString() before executeQuery() prints out the sql as per the postgres example, I can cut and past it into psql and it runs fine. Both ?::date and CAST(? AS date) fail in java. ?::date works fine in psql, but CAST(? AS date) fails in psql. SELECT (date CAST('2001-02-16' AS date), date ('2001-12-21')) OVERLAPS (date ('2001-10-30'), date ('2002-10-30')); ERROR: syntax error at or near "CAST" I have achieved my main aim, to put a work around on the net, so it should turn up in a search. As I could not find one while looking at the opening post on this forumn I think i had an issue with setDate as well, but may have been just my poor java. Thanks guys for your replies. On Tue, May 31, 2011 at 11:52, Tom Lane wrote: > Simon Mitchell writes: > > // some bad code > > sqlText = "SELECT (date ?, date ?) OVERLAPS (date ?, date ?);"; > > That's not going to work, because the syntax > date 'something' > only works for literal constants, and a ? marker is not a literal constant. > You could try > ?::date > or > CAST(? AS date) > if you prefer to avoid Postgres-specific syntax. Even better, don't use > setString --- that's really only meant for parameters that are of string > datatypes. > > regards, tom lane > -- simon --0016364c7efff541e404a489c6e8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
This is a very minor issue, the prob= lem only occurs with the prepared statement, a query string without prepare= d works fine. I was just following it up as it was a problem posted on an A= ustralian=A0forum, the original issue has been resolved.

=A0

Anyway, so the sql I am using is just based on the overlaps exam= ple from the postgresql doc=A0http://www.postgresql.org/docs/9.0/static/function= s-datetime.html

The strange thing is the doing a toString() before executeQuery() = prints out the sql as per the postgres example, I can cut and past it into = psql and it runs fine.

Both=A0=A0 ?::date and=A0=A0CAST(? AS date) fail in java.

=A0 =A0?::date works fine in psql, but =A0CAST(? AS = date) fails in psql.

SELECT (date CAST(= '2001-02-16' AS date), date ('2001-12-21')) OVERLAPS (date = ('2001-10-30'), date ('2002-10-30'));
ERROR: =A0syntax error at or near "CAST"
I have achieved my main aim, to put a work around on the net, s= o it should turn up in a search. As I could not find one while looking at t= he opening post on this forumn

I think i had an issue with setDate as well, but may ha= ve been just my poor java.

Thanks guys for your re= plies.

On Tue, May 31, 2011 at 11:= 52, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Simon Mitchell <simon@jseb.com> writes:
> // some bad code
> =A0 =A0 sqlText =3D "SELECT (date ?, date ?) OVERLAPS (date ?, da= te ?);";

That's not going to work, because the syntax
=A0 =A0 =A0 =A0date 'something'
only works for literal constants, and a ? marker is not a literal constant.=
You could try
=A0 =A0 =A0 =A0?::date
or
=A0 =A0 =A0 =A0CAST(? AS date)
if you prefer to avoid Postgres-specific syntax. =A0Even better, don't = use
setString --- that's really only meant for parameters that are of strin= g
datatypes.

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0regards, tom lane



--
simon
--0016364c7efff541e404a489c6e8--