Convert Unixtime to Datetime SQL (Oracle)

荒凉一梦 提交于 2019-12-17 04:35:19

问题


I have a datetime field(P_DT) and I would like to return all results where P_DT is greater then an input unix timestamp.

Does Oracle have any built in functions that can help?

In my searchs I find resuts for DateTime to Unix but no Unix to DateTime...


回答1:


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 unix_ts_to_date( p_unix_ts IN NUMBER )
  RETURN DATE
IS
  l_date DATE;
BEGIN
  l_date := date '1970-01-01' + p_unix_ts/60/60/24;
  RETURN l_date;
END;

which you can see being called

SQL> select unix_ts_to_date( 1336822620 ) from dual;

UNIX_TS_TO_DATE(133
-------------------
2012-05-12 11:37:00



回答2:


I used this in the end...

date=unixtimestamp number

to_date(\'1970-01-01\',\'YYYY-MM-DD\') + numtodsinterval('.$_GET["date"].',\'SECOND\')


来源:https://stackoverflow.com/questions/10554405/convert-unixtime-to-datetime-sql-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!