Re: Cast: timestamp to integer

From: Weiping He <laser(at)pgsqldb(dot)com>
To: Michal Durys <joe(at)anime(dot)com(dot)pl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Cast: timestamp to integer
Date: 2003-06-12 17:14:21
Message-ID: 3EE8B4ED.1060200@pgsqldb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

seems:

select cast(extract(epoch from current_timestamp) as integer);

is much simplier.

Michal Durys wrote:

>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.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Erik Price 2003-06-12 17:14:59 LAST_INSERT_ID equivalent
Previous Message Steve Crawford 2003-06-12 17:06:44 pg_conf idea (was Re: Postgres performance comments from a MySQL user)