This question is pretty much the opposite of converting Unixtime to Datetime SQL (Oracle).
As Justin Cave says:
There are no built-in functions. But it is relatively easy to write. Since Unix timestamp is the number of seconds since January 1, 1970
Since subtracting one date from another results in the number of days between them, you can do something like:
create or replace function date_to_unix_ts( PDate in date ) return number is l_unix_ts number; begin l_unix_ts := ( PDate - date '1970-01-01' ) * 60 * 60 * 24; return l_unix_ts; end;
Since in seconds since 1970, the number of fractional seconds does not matter. You can still call it with the timestamp data type, though ...
SQL> select date_to_unix_ts(systimestamp) from dual; DATE_TO_UNIX_TS(SYSTIMESTAMP) ----------------------------- 1345801660
Sorry for your comment, but I don't see this behavior:
SQL> with the_dates as ( 2 select to_date('08-mar-12 01:00:00 am', 'dd-mon-yy hh:mi:ss am') as dt 3 from dual 4 union all 5 select to_date('08-mar-12', 'dd-mon-yy') 6 from dual ) 7 select date_to_unix_ts(dt) 8 from the_dates 9 ; DATE_TO_UNIX_TS(DT) ------------------- 1331168400 1331164800 SQL>
There are 3600 seconds of difference, i.e. 1 hour.
Ben
source share