How to update upper-bound of tstzrange ?

Started by Laura Smithalmost 2 years ago5 messagesgeneral
Jump to latest
#1Laura Smith
n5d9xq3ti233xiyif2vp@protonmail.ch

Could someone kindly help me out with the correct syntax ?

My first thought was the below but that doesn't work:

update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where bar_id='abc';
ERROR: syntax error at or near "("
LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

#2Kashif Zeeshan
kashi.zeeshan@gmail.com
In reply to: Laura Smith (#1)
Re: How to update upper-bound of tstzrange ?

Hi

Try this one.

UPDATE foo
SET bar_times = bar_times + INTERVAL '1 hour'
WHERE bar_id = 'abc';

Regards
Kashif Zeeshan
Bitnine Global

On Mon, May 20, 2024 at 3:30 PM Laura Smith <
n5d9xq3ti233xiyif2vp@protonmail.ch> wrote:

Show quoted text

Could someone kindly help me out with the correct syntax ?

My first thought was the below but that doesn't work:

update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where
bar_id='abc';
ERROR: syntax error at or near "("
LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

#3Erik Wienhold
ewie@ewie.name
In reply to: Laura Smith (#1)
Re: How to update upper-bound of tstzrange ?

On 2024-05-20 12:30 +0200, Laura Smith wrote:

Could someone kindly help me out with the correct syntax ?

My first thought was the below but that doesn't work:

update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where bar_id='abc';
ERROR: syntax error at or near "("
LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

Use the constructor function:

UPDATE foo SET bar_times = tstzrange(lower(bar_times), upper(bar_times) + interval '1' hour);

But this does not preserve the inclusivity/exclusivity of bounds from
the input range, so you may have to pass in the third argument as well.

https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRUCT

--
Erik

#4Laurenz Albe
laurenz.albe@cybertec.at
In reply to: Erik Wienhold (#3)
Re: How to update upper-bound of tstzrange ?

On Mon, 2024-05-20 at 13:56 +0200, Erik Wienhold wrote:

On 2024-05-20 12:30 +0200, Laura Smith wrote:

Could someone kindly help me out with the correct syntax ?

My first thought was the below but that doesn't work:

update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where bar_id='abc';
ERROR: syntax error at or near "("
LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

Use the constructor function:

UPDATE foo SET bar_times = tstzrange(lower(bar_times), upper(bar_times) + interval '1' hour);

But this does not preserve the inclusivity/exclusivity of bounds from
the input range, so you may have to pass in the third argument as well.

https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRUCT

If you need to preserve the information whether the upper and lower bounds
are inclusive or not, you could

UPDATE foo
SET bar_times = tstzrange(
lower(bar_times),
upper (bar_times) + INTERVAL '1 hour',
CASE WHEN lower_inc(bar_times) THEN '[' ELSE '(' END ||
CASE WHEN upper_inc(bar_times) THEN ']' ELSE ')' END
)
WHERE ...

Yours,
Laurenz Albe

#5Laura Smith
n5d9xq3ti233xiyif2vp@protonmail.ch
In reply to: Laurenz Albe (#4)
Re: How to update upper-bound of tstzrange ?

Thanks all for your answers ! Much appreciated.

Sent with Proton Mail secure email.

On Tuesday, 21 May 2024 at 11:02, Laurenz Albe <laurenz.albe@cybertec.at> wrote:

Show quoted text

On Mon, 2024-05-20 at 13:56 +0200, Erik Wienhold wrote:

On 2024-05-20 12:30 +0200, Laura Smith wrote:

Could someone kindly help me out with the correct syntax ?

My first thought was the below but that doesn't work:

update foo set upper(bar_times)=upper(bar_times)+interval '1' hour where bar_id='abc';
ERROR: syntax error at or near "("
LINE 1: update event_sessions set upper(bar_times)=upper(bar_ti...

Use the constructor function:

UPDATE foo SET bar_times = tstzrange(lower(bar_times), upper(bar_times) + interval '1' hour);

But this does not preserve the inclusivity/exclusivity of bounds from
the input range, so you may have to pass in the third argument as well.

https://www.postgresql.org/docs/current/rangetypes.html#RANGETYPES-CONSTRUCT

If you need to preserve the information whether the upper and lower bounds
are inclusive or not, you could

UPDATE foo
SET bar_times = tstzrange(
lower(bar_times),
upper (bar_times) + INTERVAL '1 hour',
CASE WHEN lower_inc(bar_times) THEN '[' ELSE '(' END ||
CASE WHEN upper_inc(bar_times) THEN ']' ELSE ')' END
)
WHERE ...

Yours,
Laurenz Albe