oracle - 将时间戳数据类型转换为 unix 时间戳 Oracle

标签 oracle oracle10g unix-timestamp

我在数据库中有一个时间戳数据类型,格式为 24-JuL-11 10.45.00.000000000 AM,想将其转换为 unix 时间戳,我怎样才能得到它?

最佳答案

这个问题几乎是 Convert Unixtime to Datetime SQL (Oracle) 的倒数

正如贾斯汀凯夫所说:

There are no built-in functions. But it's relatively easy to write one. Since a Unix timestamp is the number of seconds since January 1, 1970



由于从另一个日期中减去一个日期会导致它们之间的天数,您可以执行以下操作:
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;

由于自 1970 年以来以秒为单位,因此小数秒的数量无关紧要。不过,您仍然可以使用时间戳数据类型调用它...
SQL> select date_to_unix_ts(systimestamp) from dual;

DATE_TO_UNIX_TS(SYSTIMESTAMP)
-----------------------------
                   1345801660

针对您的评论,我很抱歉,但我没有看到这种行为:
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>

有 3,600 秒的差异,即 1 小时。

关于oracle - 将时间戳数据类型转换为 unix 时间戳 Oracle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12105691/

相关文章:

sql - 允许角色查询、插入和更新可变数组

sql - 总和减去最小值的平均值

c# - 在 Oracle DB 中获取正确的自动递增 ID

java - jdbc : front end java, 后端 Oracle 10g Express 版

php - 将 unix 时间戳存储为无符号整数会有什么好处吗?

oracle - 对 PUBLIC 数据库链接表的托管 ODP.NET 调用导致 TNS 错误

oracle - 如何运行oracle企业管理器?

mysql - 在 Mysql 中插入日期和时间作为 unix_timestamp

java - 日期递增的 Hive UDF

sql - 如何在select语句中包含双引号?