pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: David Gauthier <davegauthierpg@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: format return of "age" to hh:mm
Date: Thu, 5 Mar 2020 08:37:13 -0800
Message-ID: <40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.com> (raw)
In-Reply-To: <CAMBRECB7TGJzy5X7i3XBopz2q5qhR1Di358upEKmLukpWBjWdQ@mail.gmail.com>
References: <CAMBRECB7TGJzy5X7i3XBopz2q5qhR1Di358upEKmLukpWBjWdQ@mail.gmail.com>

On 3/5/20 7:50 AM, David Gauthier wrote:
> Hi:
> 
> How does one reformat the output of the "age" function to always be in 
> terms of hours:mins.
> 
> E.g.
> 
> dvdb=> select age('2020-03-05 01:40:32-05','2020-03-01 21:56:05-05');
>         age
> -----------------
>   3 days 03:44:27
> (1 row)
> 
> I want...
> 
> "75:44"
> 
> I'm not married to "age"  If there's a better way to do this that's fine 
> too.

Not sure it's better, but it will give you idea of what needs to be done:


SELECT
     floor(
         extract(
             epoch FROM ('2020-03-05 01:40:32-05'::timestamptz - 
'2020-03-01 21:56:05-05'::timestamptz))
             / 3600)::varchar || ':' ||
     ((mod(
           extract(
               epoch FROM ('2020-03-05 01:40:32-05'::timestamptz - 
'2020-03-01 21:56:05-05'::timestamptz))::numeric,
  3600::numeric) / 60)::int)::varchar;

?column?
----------
  75:44
(1 row)

> 
> Thanks in advance !


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (5+ messages)

Message-ID: <40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.com>
Permalink:  ../40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.com/
Also on:    postgresql.org/message-id/40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, davegauthierpg@gmail.com
  Subject: Re: format return of "age" to hh:mm
  In-Reply-To: <40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.com>
  References: <CAMBRECB7TGJzy5X7i3XBopz2q5qhR1Di358upEKmLukpWBjWdQ@mail.gmail.com> <40a237c3-e065-051b-51b6-0a6b62e12fc2@aklaver.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