From: Steven Lembark <lembark@wrkhors.com>
To: pgsql-general@lists.postgresql.org
Cc: lembark@wrkhors.com
Cc: David Gauthier <davegauthierpg@gmail.com>
Subject: Re: Convert interval to hours
Date: Fri, 14 Sep 2018 13:40:22 -0500
Message-ID: <20180914134014.18a9cf7d@wrkhors.com> (raw)
In-Reply-To: <CAMBRECAVeceZWpixy92OskN8=R9g9PXi3Vj0w2+GHFvt3vwzTg@mail.gmail.com>
References: <CAMBRECASE0aEf28DTVpY58MDrZVP9nUcTPW7NJkwPv4uVk-oAw@mail.gmail.com>
<CAP8Hi7Kt=r-pQbq5fnr-e029aLfX=ppyu+6mySxz_oe86LrMnA@mail.gmail.com>
<20180914111050.16b7af88@steamer.attlocal.net>
<CAMBRECAVeceZWpixy92OskN8=R9g9PXi3Vj0w2+GHFvt3vwzTg@mail.gmail.com>
On Fri, 14 Sep 2018 12:21:14 -0400
David Gauthier <davegauthierpg@gmail.com> wrote:
> I'm using postgres v9.5.2 on RH6.
PG can convert the times for you.
For times (not timestamps) you are always better off dealing with
either time or integer seconds. There are a variety of issues with
rouding that affect repeatability and accuracy of results using
floats or doubles. Given that 10 and three are both continuing
fractions in binary (e.g., 1/10 binary is an infinite series)
division by 3600 will only cause you annoyance at some point.
If you are subtracting times then you will (usually) end up with
an interval, which can be cast to seconds in the query and give
you precise, accurate, repeatable results every time.
e.g.,
select
extract
(
epoch from ( time1 - time2 )::interval
)
as "seconds",
...
is one approach.
In nearly all cases you are better off selecting and converting
the time in SQL rather than converting the start and end times
from numeric (time) to string (DBI) and then back from char *
to float/double or int/unsigned. The charaacter conversion is
expensive and numeric -> string -> numeric leaes you open to all
sorts of rouding and conversion issues.
Frankly, if you have to run the query more than once I'd suggest
adding a view that does the select/convert for you (along with
dealing with any NULL's that creep into things). PG makes it quite
easy to add the view and quite in-expensive to apply it.
--
Steven Lembark 3920 10th Ave South
Workhorse Computing Birmingham, AL 35222
lembark@wrkhors.com +1 888 359 3508
--
Steven Lembark 3920 10th Ave South
Workhorse Computing Birmingham, AL 35222
lembark@wrkhors.com +1 888 359 3508
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: lembark@wrkhors.com, davegauthierpg@gmail.com
Subject: Re: Convert interval to hours
In-Reply-To: <20180914134014.18a9cf7d@wrkhors.com>
References: <CAMBRECASE0aEf28DTVpY58MDrZVP9nUcTPW7NJkwPv4uVk-oAw@mail.gmail.com> <CAP8Hi7Kt=r-pQbq5fnr-e029aLfX=ppyu+6mySxz_oe86LrMnA@mail.gmail.com> <20180914111050.16b7af88@steamer.attlocal.net> <CAMBRECAVeceZWpixy92OskN8=R9g9PXi3Vj0w2+GHFvt3vwzTg@mail.gmail.com> <20180914134014.18a9cf7d@wrkhors.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