Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMDSa-0008Fd-4L for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2019 15:17:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hMDSY-0005UM-8M for pgsql-general@arkaria.postgresql.org; Thu, 02 May 2019 15:17:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMDSX-0005UF-Rc for pgsql-general@lists.postgresql.org; Thu, 02 May 2019 15:17:17 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hMDSV-0006uY-8L for pgsql-general@postgresql.org; Thu, 02 May 2019 15:17:16 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id D4D7D813; Thu, 2 May 2019 11:17:13 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 02 May 2019 11:17:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=E Bak2/l+o1sBo9W23X+iZmwFfaaR6EOBPndHKDtvR1M=; b=FWV2rtmRex6DfN9lW BP2Y+yogMAzJb5xUCPwBma+lJj94cz305a3F4KwpdtS7e9HidEL/3W9+9gYpL235 fjqf4lisV2XmVHiPgaymx379LYigAvfOoss5FFD57PrFL33/rQ9opE1Y2rvLnztz zILLS59l0a4fGzptVjc7Pb9t7Qh0HautsLSnHX8/C/jS+Hw1qC+Q8mHKWl0nsO/I vdRuc99jvGDRj20Aq70qE8dU5JUrUcFjF9spGRybPZtvkxzLIwizHnlRip+DMYx8 Zl6A+tzaxgtO1h2qiR0coe/tzgpb/fqZLfKuWBw4vcYSibV4os89xvFr+kJ5S8Bq 9hDJw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=EBak2/l+o1sBo9W23X+iZmwFfaaR6EOBPndHKDtvR 1M=; b=LCBAC1ZhnEYkAZ5tLZnSVw3FayU10wK9wgVebSCKqzSA3X/s6RthpS2Ml JJo5+K27bJ8BNSpmZLukHHzWCjSHlPEPOh69Oi8HXOQs9x3QfFYnlgseAwGn6zNB O5v3hvdyQxnhd7jaLkbbn32axeITdy3gsQW/sm8SD08FbdS13yrZk5YbZjZ/Au1B bBI825Bv1ZL0PabFWjeciiY2Sd44mV8Vpc+N7EXrBV6ztpgaVhs4lUxxMgT3/f/B 5GhsMqLADwdTsPsF2SBlgu+GL4Opz3OPE7at62Kp5Sa0t7sYQa8w152sHkbShx/D nT1gtGIs9Wd/Gi/Y9jCW4iI+tHEkw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrieelgdeklecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpeifihhkihhpvgguihgrrdhorhhgnecukfhppeejuddrvdduvddr udehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 6DF53E4894; Thu, 2 May 2019 11:17:12 -0400 (EDT) Subject: Re: Query not producing expected result To: Francisco Olarte Cc: Chuck Martin , pgsql-general References: <25696.1556728884@sss.pgh.pa.us> <1e416a02-099f-004b-aaeb-0521cd03e6bf@aklaver.com> From: Adrian Klaver Message-ID: Date: Thu, 2 May 2019 08:17:11 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/2/19 12:57 AM, Francisco Olarte wrote: > Adrian: > > On Wed, May 1, 2019 at 8:14 PM Adrian Klaver wrote: > ..... >>> select '2019-05-02'::timestamp <= '2019-05-01 24:00'::timestamp; >>> And you'll see and out of range date selected. >> >> Technically it is correct as: >> test_(postgres)# select '2019-05-02'::timestamp; >> timestamp >> --------------------- >> 2019-05-02 00:00:00 > > "Tecnhnically" is not an exact term in this context, so , ok, you right. If you want exact: test=> select '2019-05-01 24:00'::timestamp = '2019-05-02'::timestamp; ?column? ---------- t For the reason why see below. > >> which is Midnight and is both the end of one day and start of another. > > That's one definition. Of part a timestamp system on which timestamps > belong to either one or two date. Use it at your own risk. I prefer to > use one where, once the time zone is fixed ( as I see some tz stuff > coming ), timestamps belong to exactly one date, I've founds it avoids > problem, IANAL, YMMV, .... > >> It comes down to where you want to draw the line between days. > > Normally everybody wants "timestamps in 2019-04-01" to give > "2019-04-01" when converted to date. > > You can try all sort of technicisms and discussions, but the fact is > if you want the simpler/original: > > dateTime::date <= '2019-05-01'::date ( second cast is implicit usually) > > And you try to keep the <= but enable indexing by augmenting the > constant to 24:00 > > datetime <= '2019-05-01 24:00'::timestamp ( second cast auto ) > > You have changed the query, and it is the 24:00 which is nearly hiding > it. Probably explain would shot it. > > But if you get into the habit of working with <: > > datTime::date < '2019-05-02' (::date implicit ) > > The transformation is straightforward: > > dateTime < '2019-05-02' (::timestamp implicit ) Unless of course you are working with something that uses the standard below: test=> select '2019-05-01 24:00'::timestamp < '2019-05-02'::timestamp; ?column? ---------- f I know a corner case. Still what all this points out is that it comes down to a definition on the part of a user/institution as to how they are going to define the day boundary. > > Even if your constants come from user input and are complex, > transformation works, as > > sometimestamp::date < somedate ( somedate supposed to be date-typed) > > Works if you just switch the casting side: > > sometimestamp< sometdate::timestamp > > You know it. Playing with 24:00, which IIRC is just some exception, It is not just some exception it is an ISO standard: https://en.wikipedia.org/wiki/ISO_8601#Times "Midnight is a special case and may be referred to as either "00:00" or "24:00". The notation "00:00" is used at the beginning of a calendar day and is the more frequently used. At the end of a day use "24:00". "2007-04-05T24:00" is the same instant as "2007-04-06T00:00" (see Combined date and time representations below). " > and is a value that is never going to be produced on output, is nice > for quickie handwritten queries, but not a path I would recommend. In > fact if you have a date in an expression it's going to be really > akward to use, you'll have to convert a date to timestamp by piping it > through text conversions. It's, IMNSHO, much better to learn to do it > in the typed world and avoid text conversions as much as possible, > they are known to be responsible for lots of problems in the computer > bussiness. > > Francisco Olarte. > -- Adrian Klaver adrian.klaver@aklaver.com