oracle中如何将日期转成时间戳

1,函数定义法

定义

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') * 86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone), 1, 3))*3600;

return l_unix_ts;

end;

 /


调用

 select date_to_unix_ts(TO_DATE('2014-06-20 12:11:11','YYYY-MM-DD HH24:MI:SS')) as dt from dual;


2,直接转换

select ((TO_DATE('2014-06-20 12:11:11','YYYY-MM-DD HH24:MI:SS')-to_date('01-JAN-1970','DD-MON-YYYY')) * 86400 - 8*3600) as dt from dual;


3,oracle时间戳转成日期格式

select to_char(1403237471/(60 * 60 * 24)+to_date('1970-01-01 08:00:00', 'YYYY-MM-DD HH24:MI:SS'),'YYYYMM') from daul;


你可能感兴趣的:(数据库)