Re: How to add a variable to a timestamp.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Eagna <eagna(at)protonmail(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: How to add a variable to a timestamp.
Date: 2022-10-29 22:13:54
Message-ID: 85459.1667081634@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bryn Llewellyn 2022-10-30 03:20:50 Re: "peer" authentication: cannot make "pg_ident.conf" work as I believe that the doc says that it should
Previous Message Adrian Klaver 2022-10-29 20:10:24 Re: How to add a variable to a timestamp.