Oracle时间与Unix时间戳的转换

来源:互联网 发布:女性外阴部测量数据 编辑:程序博客网 时间:2024/05/14 07:59
Unix时间戳记是从'1970-01-01 00:00:00'GMT开始的秒数,表现为整数型。
to_char函数支持date和timestamp,但是trunc却不支持TIMESTAMP数据类型。

Oracle中的时间是Date型,以下函数提供了两种时间转换的Oracle函数
(1)从Unix时间戳记转换为Oracle时间
create or replace function unix_to_oracle(in_number NUMBER) return date isbegin    return(TO_DATE('19700101','yyyymmdd') + in_number/86400 +TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))/24);end unix_to_oracle;

(2)由Oracle时间Date型转换为Unix时间戳记
create or replace function oracle_to_unix(in_date IN DATE) return number is  begin    return( (in_date -TO_DATE('19700101','yyyymmdd'))*86400 - TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))*3600);end oracle_to_unix;

(3) Date转换为Timestamp要使用CAST函数,例如:
  1. select CAST(sysdate as Timestamp)  from dual;  

0 0
原创粉丝点击