pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to  add a variable to a timestamp.
4+ messages / 4 participants
[nested] [flat]

* How to  add a variable to a timestamp.
@ 2022-10-29 17:35  Eagna <eagna@protonmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Eagna @ 2022-10-29 17:35 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hi,

I'm trying to do something like this.

SELECT
d.i,
h.i,

'2022-10-31 00:00:00'::TIMESTAMP + INTERVAL 'd.i DAY'
FROM
GENERATE_SERIES(0, 6) AS d(i),
GENERATE_SERIES(0, 23) AS h(i);

where I add d.i days (and also h.i hours) to a timestamp.

I can't seem to get this to work. Any ideas appreciated.

E.

Sent with [Proton Mail](https://proton.me/) secure email.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to add a variable to a timestamp.
@ 2022-10-29 18:35  Erik Wienhold <ewie@ewie.name>
  parent: Eagna <eagna@protonmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Erik Wienhold @ 2022-10-29 18:35 UTC (permalink / raw)
  To: Eagna <eagna@protonmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

> On 29/10/2022 19:35 CEST Eagna <eagna@protonmail.com> wrote:
>
> I'm trying to do something like this.
>
> SELECT
> d.i,
> h.i,
>
> '2022-10-31 00:00:00'::TIMESTAMP + INTERVAL 'd.i DAY'
> FROM
> GENERATE_SERIES(0, 6) AS d(i),
> GENERATE_SERIES(0, 23) AS h(i);
>
> where I add d.i days (and also h.i hours) to a timestamp.
>
> I can't seem to get this to work. Any ideas appreciated.

Create the interval with make_interval(days => d.i, hours => h.i).

https://www.postgresql.org/docs/15/functions-datetime.html#id-1.5.8.15.6.2.2.28.1.1.1

--
Erik





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to add a variable to a timestamp.
@ 2022-10-29 20:10  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Eagna <eagna@protonmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2022-10-29 20:10 UTC (permalink / raw)
  To: Eagna <eagna@protonmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On 10/29/22 10:35, Eagna wrote:
> 
> Hi,
> 
> I'm trying to do something like this.
> 
> SELECT
>    d.i,
>    h.i,
> 
>   '2022-10-31 00:00:00'::TIMESTAMP  + INTERVAL 'd.i DAY'
> FROM
>    GENERATE_SERIES(0,  6) AS d(i),
>    GENERATE_SERIES(0, 23) AS h(i);
> 
> where I add d.i days (and also h.i hours) to a timestamp.
> 
> I can't seem to get this to work. Any ideas appreciated.

SELECT
   d.i,
   h.i,
  '2022-10-31 00:00:00'::TIMESTAMP  + (d.i::text || ' DAY ' || h.i::text 
|| ' HOUR')::interval
FROM
   GENERATE_SERIES(0,  6) AS d(i),
   GENERATE_SERIES(0, 23) AS h(i);

> 
> 
> E.
> 
> Sent with Proton Mail <https://proton.me/; secure email.
> 
> 

-- 
Adrian Klaver
adrian.klaver@aklaver.com






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: How to add a variable to a timestamp.
@ 2022-10-29 22:13  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2022-10-29 22:13 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Eagna <eagna@protonmail.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Adrian Klaver <adrian.klaver@aklaver.com> writes:
> On 10/29/22 10:35, Eagna wrote:
>> I'm trying to do something like this.
>>   '2022-10-31 00:00:00'::TIMESTAMP + INTERVAL 'd.i DAY'

That will not work.  A literal is a literal, you can't expect that
the system will interpret parts of it as variable references.

>   '2022-10-31 00:00:00'::TIMESTAMP  + (d.i::text || ' DAY ' || h.i::text 
> || ' HOUR')::interval

That'll work, but my what a kluge.  More recommendable is

'2022-10-31 00:00:00'::TIMESTAMP + d.i * '1 day'::interval
+ h.i * '1 hour'::interval

(Or you can spell the constants like INTERVAL '1 day',
if you prefer.)

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-10-29 22:13 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-10-29 17:35 How to  add a variable to a timestamp. Eagna <eagna@protonmail.com>
2022-10-29 18:35 ` Re: How to add a variable to a timestamp. Erik Wienhold <ewie@ewie.name>
2022-10-29 20:10 ` Re: How to add a variable to a timestamp. Adrian Klaver <adrian.klaver@aklaver.com>
2022-10-29 22:13   ` Re: How to add a variable to a timestamp. Tom Lane <tgl@sss.pgh.pa.us>

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