Cast: timestamp to integer - Mailing list pgsql-general

From Michal Durys
Subject Cast: timestamp to integer
Date
Msg-id 20030612184455.577d0a6e.joe@anime.com.pl
Whole thread Raw
Responses Re: Cast: timestamp to integer  (Rory Campbell-Lange <rory@campbell-lange.net>)
Re: Cast: timestamp to integer  (Csaba Nagy <nagy@ecircle-ag.com>)
Re: Cast: timestamp to integer  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
List pgsql-general
Hello,

I hope it's not a question for psql-novice. ;-)

I'd like to convert timestamp and date fields to intergers.
Unfortunately cast() says it's impossible:

ERROR: Cannot cast type date to integer
ERROR: Cannot cast type timestamp without time zone to integer

I'm quite sure it should be possible somehow. After all timestamp is
(AFAIR) number of seconds counted from 1970.

I need it for example to caclulate average number of downloads of a
given file a day. For the time being I came up with an ugly solution
like this:

SELECT id, name, counter, counter/(extract(years FROM age(date_add))*365
+ extract(months FROM age(date_add))*30 + extract(days FROM
age(date_add)) + 1) AS ratio  FROM dnload_files WHERE counter>0;

I will be much obliged for any suggestions.

Thanks in advance.
--
 Joe <joe@anime.com.pl>

pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Is the max size of a table limited by the max file size
Next
From: Mike Mascari
Date:
Subject: Re: Temp tables, indexes and DELETE vs. TRUNCATE