pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ron Clarke <rclarkeai@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Assigning values to a range in Pgsql and inclusive / exclusive bounds
Date: Fri, 12 Jun 2020 11:55:24 -0700
Message-ID: <3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com> (raw)
In-Reply-To: <CAGVf-sO0E-5qM2fm_oJ=FnS4E_sqBGneofwNFe2fnoVXEDGm=w@mail.gmail.com>
References: <CAGVf-sO0E-5qM2fm_oJ=FnS4E_sqBGneofwNFe2fnoVXEDGm=w@mail.gmail.com>

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





view thread (4+ messages)  latest in thread

Message-ID: <3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com>
Permalink:  ../3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com/
Also on:    postgresql.org/message-id/3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, rclarkeai@gmail.com
  Subject: Re: Assigning values to a range in Pgsql and inclusive / exclusive bounds
  In-Reply-To: <3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com>
  References: <CAGVf-sO0E-5qM2fm_oJ=FnS4E_sqBGneofwNFe2fnoVXEDGm=w@mail.gmail.com> <3d14392e-4b60-251c-af21-f2f12ebaba3d@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox