Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id DF6D0D6F1EA for ; Thu, 29 Mar 2012 06:00:58 -0300 (ADT) Received: from mailout-de.gmx.net ([213.165.64.23]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SDBE1-0006q0-KN for pgsql-jdbc@postgresql.org; Thu, 29 Mar 2012 09:00:58 +0000 Received: (qmail invoked by alias); 29 Mar 2012 09:00:44 -0000 Received: from i577A810C.versanet.de (EHLO MatthiasLaptop) [87.122.129.12] by mail.gmx.net (mp037) with SMTP; 29 Mar 2012 11:00:44 +0200 X-Authenticated: #41751617 X-Provags-ID: V01U2FsdGVkX1/oQt5QyzDoDmpcillGiWWI82cQElQIQvWmo/2nr1 U+cPmajj351aRc From: =?iso-8859-1?Q?Matthias_B=F6hm?= To: Subject: Re: Bug with PreparedStatements using EXTRACT function Date: Thu, 29 Mar 2012 11:00:45 +0200 Message-ID: <000401cd0d8a$6dbe04f0$493a0ed0$@net> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac0Nimzs9hZKRvQZS8mJfnQOi+TYFQ== Content-Language: de X-Y-GMX-Trusted: 0 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/58 X-Sequence-Number: 22797 > -----Urspr=FCngliche Nachricht----- > Von: Maciek Sakrejda [mailto:msakrejda@truviso.com] > Gesendet: Mittwoch, 28. M=E4rz 2012 22:54 > An: Matthias B=F6hm > Cc: pgsql-jdbc@postgresql.org > Betreff: Re: [JDBC] Bug with PreparedStatements using EXTRACT function >=20 > That's because the 'date' is not a cast but some sort of type > specification inherent to the expression. I don't understand the > details of the grammar, but you can see this if you try to run it via > PREPARE (the closest language-level thing you can use to JDBC's > parameterized statements): >=20 > cqdb=3D# prepare foo(date) as select extract (year from date '2000-01- > 01'); > PREPARE > cqdb=3D# prepare bar(date) as select extract (year from date $1); > ERROR: syntax error at or near "$1" > LINE 1: prepare bar(date) as select extract (year from date $1); > ^ > cqdb=3D# prepare baz(date) as select extract (year from $1); > PREPARE > cqdb=3D# OK, I found a solution: Whereas the last prepare statement works on the sql console it doesn't = work with jdbc: I've tried to construct the PreparedStatement as follows: PreparedStatement stmt2 =3D conn.prepareStatement( "SELECT EXTRACT (YEAR FROM ?)"); And now the exception I get is (translated roughly from German): Function pg_catalog.date_part(unknown, unknown) is ambiguous.=20 Hint: Couldn't choose best candidate function. You may have to add an explicit cast. Adding this explicit cast makes the thing work: PreparedStatement stmt2 =3D conn.prepareStatement( "SELECT EXTRACT (YEAR FROM CAST (? AS DATE))"); It is not obvious though at once that this cast is needed.=20 Thanks for taking a look at this issue and kind regards,=20 Matthias B=F6hm