Skip site navigation (1) Skip section navigation (2)

Re: Problem in age on a dates interval

From: Luis Sousa <llsousa(at)ualg(dot)pt>
To: "Alexander M(dot) Pravking" <fduch(at)antar(dot)bryansk(dot)ru>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>,Theodore Petrosky <tedpet5(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Problem in age on a dates interval
Date: 2004-07-20 08:17:15
Message-ID: 40FCD50B.40509@ualg.pt (view raw or flat)
Thread:
Lists: pgsql-sql
Yes, that's a much more clever solution than the one I used.
Thanks

Best regards,
Luis Sousa

Alexander M. Pravking wrote:

>On Mon, Jul 19, 2004 at 10:00:50AM +0100, Luis Sousa wrote:
>  
>
>>I worked around this problem returning the difference between the two 
>>dates, using extract doy from both.
>>Anyway, this will cause a bug on my code when changing the year. Any ideas?
>>    
>>
>
>Why don't you use the minus operator?
>
>SELECT '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp;
> ?column?
>----------
> 86 days
>
>Or, if you need the age just in days:
>
>SELECT extract(day from '2004-05-14 16:00'::timestamp - '2004-02-18 16:00'::timestamp);
> date_part
>-----------
>        86
>
>or
>
>SELECT '2004-05-14 16:00'::date - '2004-02-18 16:00'::date;
> ?column?
>----------
>       86
>
>Note that '2004-05-14 16:00'::date is actually '2004-05-14 00:00'::date,
>so the last two are not always equal.
>
>
>  
>
>>Tom Lane wrote:
>>
>>    
>>
>>>Theodore Petrosky <tedpet5(at)yahoo(dot)com> writes:
>>>
>>>
>>>      
>>>
>>>>wow.... at first I thought I had my head around a leap
>>>>year problem so I advanced your query a year....
>>>>  
>>>>
>>>>        
>>>>
>>>I think what's going on here is a difference of interpretation about
>>>whether an "M months D days" interval means to add the months first
>>>or the days first.  For instance
>>>
>>>2005-02-18 plus 2 months = 2005-04-18, plus 24 days = 2005-05-12
>>>
>>>2005-02-18 plus 24 days = 2005-03-14, plus 2 months = 2005-05-14
>>>
>>>The timestamp-plus-interval operator is evidently doing addition the
>>>first way, but it looks like age() is calculating the difference in a
>>>way that implicitly corresponds to the second way.
>>>
>>>I have some vague recollection that this has come up before, but
>>>I don't recall whether we concluded that age() needs to be changed
>>>or not.  In any case it's not risen to the top of anyone's to-do list,
>>>because I see that age() still acts this way in CVS tip.
>>>
>>>			regards, tom lane
>>>      
>>>
>
>  
>

In response to

pgsql-sql by date

Next:From: Lars Erik ThorsplassDate: 2004-07-20 10:43:30
Subject: Stored procedures and "pseudo" fields..
Previous:From: sadDate: 2004-07-20 05:36:44
Subject: locks and triggers. give me an advice please

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group