Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 30DD8B5FF84 for ; Tue, 31 May 2011 03:41:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 53807-01 for ; Tue, 31 May 2011 06:41:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [173.230.139.50]) by mail.postgresql.org (Postfix) with ESMTP id 03EEFB61B61 for ; Tue, 31 May 2011 03:41:38 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by www.postnewspapers.com.au (Postfix) with ESMTP id A89E710D; Tue, 31 May 2011 02:41:38 -0400 (EDT) X-Virus-Scanned: amavisd-new at postnewspapers.com.au.com Received: from www.postnewspapers.com.au ([127.0.0.1]) by localhost (www.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id jtVjNS0+94VC; Tue, 31 May 2011 02:41:37 -0400 (EDT) Received: from mail.postnewspapers.com.au (ppp171-128.static.internode.on.net [150.101.171.128]) by www.postnewspapers.com.au (Postfix) with ESMTP id 5A0632D; Tue, 31 May 2011 02:41:37 -0400 (EDT) Received: from [10.0.0.53] (wallace.localnet [10.0.0.53]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 5B4EAAE2FE; Tue, 31 May 2011 14:41:36 +0800 (WST) Message-ID: <4DE48DA0.5030800@postnewspapers.com.au> Date: Tue, 31 May 2011 14:41:36 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Thunderbird/3.1.9 MIME-Version: 1.0 To: Simon Mitchell CC: pgsql-jdbc@postgresql.org Subject: Re: PreparedStatement error for date parameter with overlaps References: <14646.1306806733@sss.pgh.pa.us> <4DE47C54.3070706@postnewspapers.com.au> In-Reply-To: X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/87 X-Sequence-Number: 21917 On 31/05/11 14:01, Simon Mitchell wrote: > Bingo > > thanks guys, this works fine, obviously I had no idea what i was doing, > no need to run the date function on a date. > SELECT (CAST(? AS date), ?::date) OVERLAPS (date(?), date(?)) works fine. Avoid using the type-as-function form like "date(varname)" or "date(?)". It's a non-standard hack and a postgresql implementation quirk that exists because the underlying cast implementation functions are named after their data types. Don't rely on it. Instead, use the SQL-standard "CAST(? AS date)" syntax. > So from what I understand using the date function in jdbc stuff requires > brackets like most functions do, There *is* a DATE function, but you should avoid it in favour of explicit CASTs. > but the psql is happy for me to use the date function with or without > brackets. No. The difference is that via PgJDBC you were using prepared statements. To compare PgJDBC prepared statement execution with psql, you must use the psql 'PREPARE' and 'EXECUTE' commands, not just type the SELECT in directly. See the example below. > Postgresql docs examples seem to be all with out brackets. The DATE literal prefix is only valid with *literals*, and the examples you refer to show DATE being used with literals, where it is valid. If you're using a variable or a placeholder, the DATE prefix to specify data type is *not* *valid*. Compare: -- DATE used to specify type of a literal -- regress=> SELECT DATE '2011-04-02'; date ------------ 2011-04-02 (1 row) -- DATE used to specify type of a literal in a prepared statement -- regress=> PREPARE datetest AS SELECT DATE '2011-04-02'; PREPARE regress=> EXECUTE datetest; date ------------ 2011-04-02 (1 row) -- DATE used to (try to) specify the type of a placeholder -- variable in a prepared statement. -- regress=> PREPARE datetest2 AS SELECT DATE $1; ERROR: syntax error at or near "$1" LINE 1: PREPARE datetest2 AS SELECT DATE $1; -- It is not understood, because the DATE prefix is not -- valid for a non-literal. The CAST(value AS type) syntax -- should be used instead: regress=> PREPARE datetest2 AS SELECT CAST($1 AS date); PREPARE regress=> EXECUTE datetest2('2011-04-02'); date ------------ 2011-04-02 (1 row) -- ... and it's also legal (albeit unnecessary) to explicitly specify -- the type of the literal you pass to EXECUTE, because you're using -- the typename on a literal: regress=> EXECUTE datetest2(DATE '2011-04-02'); date ------------ 2011-04-02 (1 row) Because it only applies to literals and it offers no advantage over a CAST expression, it's often best to avoid the literal-type prefix. Instead, use a CAST expression. PostgreSQL will use casts to obtain the same information as a literal-type prefix when they are used on a literal of unknown type, but unlike a type name prefix they'll also work on variables and placeholders. So, instead of: regress=> PREPARE datetest2 AS SELECT DATE $1; write regress=> PREPARE datetest2 AS SELECT CAST($1 AS date); It's the same in JDBC, except that placeholders are "?" instead of "$n". These rules don't just apply to placeholders in prepared statements. You can't use a type name to cast a variable either: SELECT DATE a FROM (SELECT TEXT '2011-04-02' a) AS x; ERROR: column "date" does not exist LINE 1: SELECT DATE a FROM (SELECT TEXT '2011-04-02' a) AS x; even though this works because it's using DATE on a literal: regress=> SELECT a FROM (SELECT DATE '2011-04-02' a) AS x; a ------------ 2011-04-02 (1 row) -- Craig Ringer