Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oFdcA-0001Y0-2h for pgsql-general@arkaria.postgresql.org; Sun, 24 Jul 2022 15:33:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oFdc8-0004lE-Sl for pgsql-general@arkaria.postgresql.org; Sun, 24 Jul 2022 15:33:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oFdc7-0004gb-2F for pgsql-general@lists.postgresql.org; Sun, 24 Jul 2022 15:33:52 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oFdc4-0001fH-An for pgsql-general@postgresql.org; Sun, 24 Jul 2022 15:33:50 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 2FB285C00D0; Sun, 24 Jul 2022 11:33:46 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Sun, 24 Jul 2022 11:33:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1658676826; x= 1658763226; bh=9pwtf0nOonnh1z6U+O9IIzqMhwXmuKHkaUVfkl/DbC0=; b=l DVpShTL2GT0O5higJL/qbdCZLvyVXBOdkfRLrOfse4z2LnZLVek+juUMuO8PCo3J jpvp3Euz1zZwJR715ypcLDrWFKwNipjKfLww+OgBsqyDSd1uQqjYeQqf+O9ehXbX 16XvfuskjKh9nuut9vuTh4vrDo0IJaArvgp94jul6raeriVnau6EDK7muVOkcB1W FCz6GtB/WU3yKQiVQc7GPW8pOc0uj76nCJMeI9A6dyBa8YJgHzfojdrbWRK/nkKM B6YnOJZmf+qp5+yDU8x5O0rC846qCPLz2qLgr0kxU2hqJQNyv8/PQQe5/YaOzjuA uxmH4bhuItw8JvM/MxlxA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1658676826; x=1658763226; bh=9 pwtf0nOonnh1z6U+O9IIzqMhwXmuKHkaUVfkl/DbC0=; b=cwGOP6DGVsHA4wscd UkPMs7XecXyjWX2M982TIdO94swvpovkZ3hmRP7lrDuFN29dYCbgVJ1uxXFTDRh1 v3EQCevu8bw4LybhVpy6hj8XdmeFijbRCum9Iw1ihiPKS8k3YQJLrXwBsnHuK3yr pmRgfBkvKkXrx7kTodpNPCcgkTbf7B/SWiY0gBaZUlb9EEay64AO7nTVrNhWZsUp GMQ0VKUW0jE/7SViphxSocguhyLgftmCTDRvqqHIvkTdH+Gj4tuuBMy459LRXb/6 QZ1cK8KsqcXYv9eubo3AKyKntLG+GcdgiSxA0nwUIc9m7qKz0tICWE1kefa4fLDV G/G0w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvddtiedgledtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtke ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepudfgueelge evfffhgeeljeehhfettdegieelgefgheefffefveegjedtueetffdtnecuvehluhhsthgv rhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Jul 2022 11:33:45 -0400 (EDT) Message-ID: <0fcbe18f-5fd4-13e2-b827-3f25ec9563c4@aklaver.com> Date: Sun, 24 Jul 2022 08:33:44 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Strange behavior between timestamp and date comparison Content-Language: en-US To: Ludwig Isaac Lim , "pgsql-general@postgresql.org" References: <1485906141.916850.1658570664512.ref@mail.yahoo.com> <1485906141.916850.1658570664512@mail.yahoo.com> From: Adrian Klaver In-Reply-To: <1485906141.916850.1658570664512@mail.yahoo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/23/22 03:04, Ludwig Isaac Lim wrote: > Hello: > > Below is a sample case that exhibits a behavior that I can't explain: > > -- create the table > create table ts (t timestamp without time zone); > > -- populate > insert into ts(t) values ('2022-07-16 00:22:06.974000'); > insert into ts(t) values ('2022-07-16 00:22:06.974000'); > insert into ts(t) values ('2022-07-16 00:22:06.974000'); > > > > -- This one return expected results > select * from ts where t::date between '2022-07-16'::Date - make_interval(days => 30) and '2022-07-16'::Date; >             t > ------------------------- >  2022-07-16 00:22:06.974 >  2022-07-16 00:22:06.974 >  2022-07-16 00:22:06.974 > (3 rows) > > > -- This one doesn't return anything (unexpected) >  select * from ts where t between '2022-07-16'::Date - make_interval(days => 30) and '2022-07-16'::Date; >  t > --- > (0 rows) Because: select '2022-07-16 00:22:06.974'::date; date ------------ 2022-07-16 select '2022-07-16 00:22:06.974'::timestamp; timestamp ------------------------- 2022-07-16 00:22:06.974 and: select '2022-07-16'::date::timestamp; timestamp --------------------- 2022-07-16 00:00:00 When you normalize all the values to a date it works e.g. t::date. When you don't then the date values in the between get compared as timestamps and 2022-07-16 00:22:06.974 is greater then 2022-07-16 00:00:00 > > -- version > select version(); >                                                  version > --------------------------------------------------------------------------------------------------------- >  PostgreSQL 14.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-15), 64-bit > (1 row) > > > > Regards, > Ludwig Lim > > > -- Adrian Klaver adrian.klaver@aklaver.com