I need to convert sysdate to a 13 digit number (Unix time stamp number) in oracle. please share the sql for this.
example: 1486015200000 Date: 02/02/2017
for number reference, use 1486015200000 in below link http://www.timestampconvert.com/
I need to convert sysdate to a 13 digit number (Unix time stamp number) in oracle. please share the sql for this.
example: 1486015200000 Date: 02/02/2017
for number reference, use 1486015200000 in below link http://www.timestampconvert.com/
The way unix generates timestamps, it's the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of Jan. 1, 1970.
You should be able to use
select (
timestamp '1970-01-01 00:00:00 GMT' + numtodsinterval(1486015200000 /1000, 'SECOND'))
at LOCAL
from dual;
In principle this is easy: Take any date. Subtract the date literal date '1970-01-01'
. Enclose the difference in parentheses and multiply by 86400000.
That is: the date difference (in Oracle) is in days. A day has 24 * 24 * 60 = 86,400 seconds, and 86,400,000 milli-seconds. The 13-digit "unix time" is the number of milliseconds elapsed since midnight January 1, 1970 UTC.
The computation on the website you linked to is off by 6 hours, so I assume you live somewhere in the Central time zone in the U.S. (or perhaps somewhere else in the same time zone, in North or Central or South America). The web site assumes the input is IN YOUR TIME ZONE. In Oracle, dates are abstract, they are not "in a time zone."
select sysdate, (sysdate - date '1970-01-01') * 86400000 as unix_time from dual;
SYSDATE UNIX_TIME
------------------- -------------
03/16/2017 15:17:32 1489677452000
This should work:
SELECT (COLUMN_WITH_ORACLE_DATE_VALUE - TO_DATE('1970-01-01', 'YYYY-MM-DD')) * 86400000 FROM DUAL;
example:
SELECT (TO_DATE('02/02/2017','MM/DD/YYYY') - TO_DATE('01/01/1970', 'MM/DD/YYYY')) * 86400000 FROM DUAL;