In MySQL this is super simple using unix_timestamp(now())
With Oracle it is a pain in the ass so I thought I'd share (yes this accounts for timezones different than GMT):
UPDATE! Does not account for timezones that break semi-hourly
select (sysdate - to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS')) * 86400 - substr(TZ_OFFSET(sessiontimezone),1,instr(TZ_OFFSET(sessiontimezone),':')-1) * 3600 from dual;
May want to set this first:
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
select from_tz(cast(to_date('1970-01-01 00:00:00','YYYY-MM-DD HH24:MI:SS') as timestamp),'UTC') at local + numtodsinterval(1208906459,'SECOND') from dual;
substituting your own seconds from EPOCH for this example: 1208906459
With default NLS_DATE_FORMAT, this won't work:
SQL> desc banner_dailies;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(38)
DAYDATE NOT NULL DATE
IMP NOT NULL NUMBER(38)
CTS NOT NULL NUMBER(38)
ACQ NOT NULL NUMBER(38)
REV_TYPE_ID NOT NULL CHAR(1)
SQL>
insert into banner_dailies values (45,'2008-04-20',1,2,3,'t')
*
ERROR at line 1:
ORA-01861: literal does not match format string
With a little magic it will work (no TO_DATE() function needed):
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'; Session altered. SQL> insert into banner_dailies values (45,'2008-04-20',1,2,3,'t'); 1 row created. SQL>
and even this:
insert into banner_dailies values (45,'20080420164442',1,2,3,'t');
So it will be compatible with MySQL's flexible date formats and further, you won't need to concern yourself so much if the applications you're supporting will be expected to insert from MySQL DATE or DATETIME types (though this will not work for YEAR types).
Cheers - Reed Sandberg (Posted at 22-APR-08 04.20.59.000000000 PM -07:00 or 1208906459)