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 1jjoq8-0002iZ-Rr for pgsql-general@arkaria.postgresql.org; Fri, 12 Jun 2020 18:55:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jjoq7-00011r-Iy for pgsql-general@arkaria.postgresql.org; Fri, 12 Jun 2020 18:55:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jjoq5-00011k-UA for pgsql-general@lists.postgresql.org; Fri, 12 Jun 2020 18:55:43 +0000 Received: from [66.111.4.26] (helo=out2-smtp.messagingengine.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jjoq1-0002lW-Us for pgsql-general@postgresql.org; Fri, 12 Jun 2020 18:55:41 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 457675C00B5; Fri, 12 Jun 2020 14:55:26 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 12 Jun 2020 14:55:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=n Fu6i5DPUNN5q9uJ1MPgNIzuRk9Z8/oRD4HIxLLyxCA=; b=wbSW0NULonGZGbJ74 +58ufoDnHLOfOvINuwFv7SlPrsDfXwR/dy+n/+nvbhHB90pjcR+WDHPex7ARzPhq 46Kgh38G1MsLWo1M4IMqdOwnfBlD3fGcCGzgrZ9gDnLiiDC7NClFuL8/zUqWurW3 B1IUM+iPP7SG/pubU53cgZ9d3cQWSzGpoTOLxHFy95Y1jdez8tZxRmuPxZLoa5h6 4RweaNsyywbP8p2vrxBGIoGYiZQEv5er8BHCvXt536j5jkNlJITTtuDO5uZ70BkP RL+4ibxZyaEmC/5iO12mk9I3NmDc2vSKmyUVvZLIAXk1ZfkPgaOxWVQT0hBuGIh5 Pw9ag== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=fm3; bh=nFu6i5DPUNN5q9uJ1MPgNIzuRk9Z8/oRD4HIxLLyx CA=; b=ZJDv+BeUFjgTnc3LO//QiuDM/m926ee1wK7Bx5esg9yJODdxov44m5+WB 6p/2yYpodUwhk4Eq9U8+zMTNIAGg4/8rqYpNB+4m02LUiCjSu3zooFLlC1flTzCY BnhRS99rHfeUGb9CXaB86eRDZb1nyXugro6WwyUjaqwHtpj57ksr2qcEP2O+tvmN 0iWDZ2EcC6qgodEvxBVaxBPkvfdEiW0ACOPmJdtm2Z139JZvlvXchgifnKPrYawO A+MRDdNGdawR3E6ubL9Ukt2/bj1bQBaWhpD8JW5S5U0DYGO+Hs8QPPYwOigbDoiL LJ3PCx93kV5/Efrlp0GmiI7dkODuQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduhedrudeiuddgudefgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeevveefve efueegffekkefhgeeiueffgfetueejvdfghfeujeejhfekjeelgedtgeenucfkphepjeeh rddujedvrdehrddvfedunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id A758F3280060; Fri, 12 Jun 2020 14:55:25 -0400 (EDT) Subject: Re: Assigning values to a range in Pgsql and inclusive / exclusive bounds To: Ron Clarke , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com> Date: Fri, 12 Jun 2020 11:55:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 66.111.4.26 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 6/12/20 11:45 AM, Ron Clarke wrote: > Hi, > > I've got a simple problem, but I'm convinced that there must be an > elegant solution. I'm a refugee from the world of MSSQL, so I'm still > finding some aspects of PostgreSQL alien. > > I'm trying to use the /tstzrange /datatype. My issue is correctly > setting the bound types when assigning values to a range in code (PGSQL). > > So if i declare this : e.g. > > /declare tx tstzrange := '[today, tomorrow)' ;/ > > > I get the variable tx as expected with the Inclusive '[' lower bound and > exclusive upper ')' bound. > > But if I attempt to reassign the value in code within pgsql I can do > this simply, only with '(' syntax for the lower bound i.e. with an > exclusive lower bound, e.g so this works:- > > /tx= (Timestamptz 'today', timestamptz 'now' + interval '1 hour');/ > > but if I try > /tx= [Timestamptz 'today', timestamptz 'now' + interval '1 hour'); / > / > / > this will have syntax errors - as the hidden 'select [' upsets the > parser.   I've tried to include a '[)' in variations of the expression, > but just get various syntax errors.. > > I've tried many combinations and I can get it to work using casts and > concatenations, e.g. :- > > / tx = CONCAT('[', 'today'::Timestamptz,',',('now' ::Timestamptz + > interval '1 hour'):: timestamptz , ')'):: tstzrange ;/ > > works but I can't help thinking that I'm missing something much simpler > and more elegant. > How should this actually be done? > > Thanks in advance for your advice. If: select tstzrange('today', 'tomorrow', '[)'); tstzrange ------------------------------------------------------- ["06/12/2020 00:00:00 PDT","06/13/2020 00:00:00 PDT") then: tx tstzrange := tstzrange('today', 'tomorrow', '[)') ; Not tested. > > Ron > Stay safe everyone. > > > here's an example script to show what I mean:- > > /do > //$$ > //DECLARE > / > > /tx tstzrange := '[today, tomorrow)' ;/ > > /answer text;/ > > /BEGIN > / > > /RAISE NOTICE 'Start %', tx;/ > > /answer = tx @> 'today'::Timestamptz;/ > > /RAISE NOTICE 'today  %', answer;/ > > /answer = tx @> 'tomorrow'::Timestamptz;/ > > /RAISE NOTICE 'tomorrow  %', answer;/ > > /-- ( works > -- tx= (Timestamptz 'today', timestamptz 'now' + interval '1 hour'); > /-- [ doesn't work > -- tx= [Timestamptz 'today', timestamptz 'now' + interval '1 hour'); > -- working around the parser?? > /tx = CONCAT('[', 'today'::Timestamptz,',',('now' ::Timestamptz > + interval '1 hour'):: /timestamptz , ')'):: tstzrange ; > > /RAISE NOTICE 'reassign  %', tx;/ > > /answer = tx @> 'today'::Timestamptz;/ > > /RAISE NOTICE 'today  %', answer;/ > > /answer = tx @> 'now'::Timestamptz;/ > > /RAISE NOTICE 'now    %', answer;/ > > /END;/ > /$$ / > -- Adrian Klaver adrian.klaver@aklaver.com