X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2D382D1B454 for ; Fri, 9 Jan 2004 05:50:38 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 47626-08 for ; Fri, 9 Jan 2004 01:49:48 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id B650CD1B438 for ; Fri, 9 Jan 2004 01:49:47 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id i095nj19020968; Fri, 9 Jan 2004 00:49:47 -0500 (EST) To: Lance Munslow Cc: "'pgsql-sql@postgresql.org'" Subject: Re: Date select question... In-reply-to: <1536369C345BD4118148000629C9833D57EAB5@nifty.preston.traveltech.co.uk> References: <1536369C345BD4118148000629C9833D57EAB5@nifty.preston.traveltech.co.uk> Comments: In-reply-to Lance Munslow message dated "Thu, 08 Jan 2004 13:13:58 +0000" Date: Fri, 09 Jan 2004 00:49:45 -0500 Message-ID: <20967.1073627385@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200401/65 X-Sequence-Number: 16458 Lance Munslow writes: > [ why do these act differently: ] > select * from test where test_date between '20041001' and '20041101'; > select * from test where test_date between 20041001 and 20041101; In the latter case the constants are integers, not dates. IMHO the best policy would be for Postgres to reject it as an invalid operation, since there are no date-vs-integer comparison operators. What you are actually getting though is an implicit coercion of both sides to text followed by a textual comparison, as you can see if you look at EXPLAIN output: regression=# create table test(test_date date); CREATE TABLE regression=# explain select * from test where test_date between '20041001' and '20041101'; QUERY PLAN ------------------------------------------------------------------------------------- Seq Scan on test (cost=0.00..25.00 rows=5 width=4) Filter: ((test_date >= '2004-10-01'::date) AND (test_date <= '2004-11-01'::date)) (2 rows) regression=# explain select * from test where test_date between 20041001 and 20041101; QUERY PLAN ------------------------------------------------------------------------------------------------- Seq Scan on test (cost=0.00..30.00 rows=5 width=4) Filter: (((test_date)::text >= '20041001'::text) AND ((test_date)::text <= '20041101'::text)) (2 rows) I've been harping for awhile on the notion that having all these implicit cross-type-category coercions to text is Evil And Dangerous, and this is another example in support of that theory. But I fully expect a lot of people to scream loudly if we disable these implicit coercions. You can bet there is someone out there who thinks he should be able to do select 'Today is ' || current_date; without having to write an explicit cast to text. regards, tom lane