Zeitdifferenzen und Konvertierung

Hallo zusammen,

wie bekomme ich einen Wert in Tagen (integer) zwischen 2 Datumswerten?

Ich probiere:
test=# select age ( TIMESTAMP ‘2009-02-28’ , date_trunc( ‘day’ , now() ));
age

2 mons 11 days
(1 Zeile)

test=# select age ( TIMESTAMP ‘2009-03-01’ , date_trunc( ‘day’ , now() ));
age

2 mons 15 days

Das ist ja völliger Unfug :slight_smile: Nur einen Tag später und schon sind es in der Differenz 4 Tage???

Egal, was ich eigentlich brächte wäre ja nun die Differenz als Integer!
Also sollte heute (2008-12-17) eigentlich 73 bzw 74 rauskommen.

Danke



test=# select '2009-03-01'::date - current_date;
 ?column?
----------
       74
(1 row)

test=# select '2009-02-28'::date - current_date;
 ?column?
----------
       73
(1 row)

Andreas

Um den Unfug mal zu erklären:

12:44 < RhodiumToad> akretschmer: the way to think about it is this: starting from the _later_ of the two dates, step back by as many whole months as you
                     can, and that's the "months" part
12:44 < RhodiumToad> akretschmer: then from that date, step back by days until you reach the earlier date
12:46 < RhodiumToad> akretschmer: so for 2009-02-28, you go 2 months to 2008-12-28, and then 11 days to 2008-12-17
12:46 < akretschmer> ahh
12:46 < RhodiumToad> akretschmer: for 2009-03-01, you go 2 months to 2009-01-01, and then 14 days to 2008-12-17
12:46 -!- bin10101 (n=aars@rrcs-24-153-239-89.sw.biz.rr.com) has joined #postgresql
12:46 < RhodiumToad> akretschmer: it's all down to the variable length of months, of course

Andreas