Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id BB0999FB319 for ; Thu, 5 Oct 2006 01:39:45 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 15598-10 for ; Thu, 5 Oct 2006 01:39:43 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from py-out-1112.google.com (py-out-1112.google.com [64.233.166.181]) by postgresql.org (Postfix) with ESMTP id A97099FB207 for ; Thu, 5 Oct 2006 01:39:42 -0300 (ADT) Received: by py-out-1112.google.com with SMTP id d80so551312pyd for ; Wed, 04 Oct 2006 21:39:40 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:cc:in-reply-to:mime-version:content-type:references:x-google-sender-auth; b=LAjrTZQGA45V6MP5CKUkT1cuUkb9UrLgk6BDXsiLc0aDuX0WUm/Xp1JJYUpN39c4dUuU6kd32w016EhbEnFA4W/2LSHOUjiDVlfsDyoVnXxG4tOUJo2cZvsqVQMEV18F1poWTkdyyv6My9yxkEJWvojEcEO2XBD5tuv3DznjR2E= Received: by 10.35.132.13 with SMTP id j13mr2738483pyn; Wed, 04 Oct 2006 21:39:40 -0700 (PDT) Received: by 10.35.78.6 with HTTP; Wed, 4 Oct 2006 21:39:40 -0700 (PDT) Message-ID: Date: Wed, 4 Oct 2006 23:39:40 -0500 From: "Aaron Bono" To: gdavis@refractions.net Subject: Re: formatting intervals with to_char Cc: pgsql-sql@postgresql.org In-Reply-To: <4523F764.80705@refractions.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_2626_26312773.1160023180711" References: <4522EC42.1080500@refractions.net> <4523F764.80705@refractions.net> X-Google-Sender-Auth: 4adc41055aa5c7c7 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.419 tagged_above=0 required=5 tests=AWL, DNS_FROM_RFC_ABUSE, HTML_MESSAGE, SPF_PASS X-Spam-Level: X-Archive-Number: 200610/54 X-Sequence-Number: 26443 ------=_Part_2626_26312773.1160023180711 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/4/06, Graham Davis wrote: > > I haven't heard any replies from this, so in the meantime I've found a > hacky way to get the output I desire. I'm basically calculating the > hours on the fly and piecing together a formatted string with > concatenations like this: > > SELECT > (((EXTRACT(day from time_idle)*24)+EXTRACT(hour from time_idle)) || ':' || > EXTRACT(minute from time_idle) || ':' || EXTRACT(second from > time_idle))::interval AS myinterval > FROM > ( SELECT ('2006-09-15T23:59:00'::timestamp - '2006-09-01 > 09:30:41'::timestamp) AS time_idle) FROM_TABLE; > > If anyone knows a better/proper way to get this result, let me know. That would be my approach though you could also replace: EXTRACT(minute from time_idle) || ':' || EXTRACT(second from time_idle) with: to_char(time_idle, ':MM:SS') If you really want to clean up your SQL you could create a function that takes any timestamp and then make the function IMMUTABLE to tell postgresql it doesn't need to rederive the results every time. ================================================================== Aaron Bono Aranya Software Technologies, Inc. http://www.aranya.com http://codeelixir.com ================================================================== ------=_Part_2626_26312773.1160023180711 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/4/06, Graham Davis <gdavis@refractions.net> wrote:
I haven't heard any replies from this, so in the meantime I've found a
hacky way to get the output I desire.  I'm basically calculating the
hours on the fly and piecing together a formatted string with
concatenations like this:

SELECT
(((EXTRACT(day from time_idle)*24)+EXTRACT(hour from time_idle)) || ':' ||
    EXTRACT(minute from time_idle) || ':' || EXTRACT(second from
time_idle))::interval AS myinterval
FROM
( SELECT ('2006-09-15T23:59:00'::timestamp - '2006-09-01
09:30:41'::timestamp) AS time_idle) FROM_TABLE;

If anyone knows a better/proper way to get this result, let me know.

 
That would be my approach though you could also replace:

EXTRACT(minute from time_idle) || ':' || EXTRACT(second from time_idle)

with:

to_char(time_idle, ':MM:SS')

If you really want to clean up your SQL you could create a function that takes any timestamp and then make the function IMMUTABLE to tell postgresql it doesn't need to rederive the results every time.

==================================================================
   Aaron Bono
   Aranya Software Technologies, Inc.
   http://www.aranya.com
   http://codeelixir.com
================================================================== ------=_Part_2626_26312773.1160023180711--