Re: date arithmetic with columns

Lists: pgsql-sql
From: Peter Faulks <faulksp(at)iinet(dot)net(dot)au>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: date arithmetic with columns
Date: 2012-02-29 19:50:39
Message-ID: 4F4E818F.9030402@iinet.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

I have two columns in two distinct tables, one is the starting time of
an event, timestamp without time zone. Data is the utc datetime (for
sorting across time zones), the other is the number of minutes to add.

I am migrating from Firebird. One of the queries uses the dateadd
function to build a local starting time thus:

SELECT CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS ts
FROM races r JOIN tracks t ON t.trk = r.trk
JOIN timezones tz on tz.state = t.state....

The equivalent postgres would be along the lines of

SELECT r.utc + INTERVAL '480 minutes'

How can I substitute the hard-coded 480 for the tz.diffmins?


From: Peter Faulks <faulksp(at)iinet(dot)net(dot)au>
To: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: date arithmetic with columns
Date: 2012-02-29 23:21:41
Message-ID: 4F4EB305.7010409@iinet.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Bit more googling and I came up with:

r.utc + CAST( tz.diffmins || ' ' || 'minutes' AS interval)

It works, but is it the best way?

On 1/03/2012 6:50 AM, Peter Faulks wrote:
> I have two columns in two distinct tables, one is the starting time of
> an event, timestamp without time zone. Data is the utc datetime (for
> sorting across time zones), the other is the number of minutes to add.
>
> I am migrating from Firebird. One of the queries uses the dateadd
> function to build a local starting time thus:
>
> SELECT CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS ts
> FROM races r JOIN tracks t ON t.trk = r.trk
> JOIN timezones tz on tz.state = t.state....
>
> The equivalent postgres would be along the lines of
>
> SELECT r.utc + INTERVAL '480 minutes'
>
> How can I substitute the hard-coded 480 for the tz.diffmins?
>


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Peter Faulks <faulksp(at)iinet(dot)net(dot)au>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: date arithmetic with columns
Date: 2012-03-03 10:24:27
Message-ID: CAFj8pRBYC0oAw_JUs6rVTFFbxQejug-S5epZEWj=-EXA2U2yeg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hello

2012/3/1 Peter Faulks <faulksp(at)iinet(dot)net(dot)au>:
> Bit more googling and I came up with:
>
> r.utc + CAST( tz.diffmins || ' ' || 'minutes' AS interval)
>
> It works, but is it the best way?
>

r.utc + tz.diffmins * interval '1 minute'

regards

Pavel Stehule

>
> On 1/03/2012 6:50 AM, Peter Faulks wrote:
>>
>> I have two columns in two distinct tables, one is the starting time of
>> an event, timestamp without time zone. Data is the utc datetime (for
>> sorting across time zones), the other is the number of minutes to add.
>>
>> I am migrating from Firebird. One of the queries uses the dateadd
>> function to build a local starting time thus:
>>
>> SELECT CAST(DATEADD(tz.diffmins MINUTE TO r.utc) AS TIME) AS ts
>> FROM races r JOIN tracks t ON t.trk = r.trk
>> JOIN timezones tz on tz.state = t.state....
>>
>> The equivalent postgres would be along the lines of
>>
>> SELECT r.utc + INTERVAL '480 minutes'
>>
>> How can I substitute the hard-coded 480 for the tz.diffmins?
>>
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql


From: hari(dot)fuchs(at)gmail(dot)com
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: date arithmetic with columns
Date: 2012-03-04 09:45:18
Message-ID: 87hay4pump.fsf@hf.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Peter Faulks <faulksp(at)iinet(dot)net(dot)au> writes:

> I have two columns in two distinct tables, one is the starting time of
> an event, timestamp without time zone. Data is the utc datetime (for
> sorting across time zones), the other is the number of minutes to add.

Maybe I'm missing something, but why don't you just use timestamp with
timezone instead?


From: Peter Faulks <faulksp(at)iinet(dot)net(dot)au>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: date arithmetic with columns
Date: 2012-03-04 11:29:23
Message-ID: 4F535213.7070402@iinet.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Good question.

I'm porting a (never actually finished) app from Firebird to Postgres.
Now that I've re-read how the timestamptz (which Firebird doesn't have)
actually works, I think I'll change the tables and get rid of the
timezone lookup.

Thanks

On 4/03/2012 8:45 PM, hari(dot)fuchs(at)gmail(dot)com wrote:
> Peter Faulks<faulksp(at)iinet(dot)net(dot)au> writes:
>
>> I have two columns in two distinct tables, one is the starting time of
>> an event, timestamp without time zone. Data is the utc datetime (for
>> sorting across time zones), the other is the number of minutes to add.
>
> Maybe I'm missing something, but why don't you just use timestamp with
> timezone instead?
>
>