Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle current_timestamp to seconds conversion

Tags:

oracle

We are using Oracle database.

In our table timestamp is stored as seconds since 1970, how can I convert the time stamp obtained through current_timestamp() function to seconds

like image 485
Sirish Avatar asked May 04 '11 09:05

Sirish


People also ask

How can I find the difference between two timestamps in Oracle seconds?

To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND .

What is FF in Oracle TIMESTAMP?

Instead, you need to use the TIMESTAMP datatype instead of the DATE datatype. The TIMESTAMP datatype has fractional seconds, as noted by the FF notation: 'DD-Mon-YYYY HH24:MI:SS.FF' For example, you can define a TIMESTAMP column and store data into it, something like this: create table t1 (timecol TIMESTAMP);

What is the difference between Systimestamp and CURRENT_TIMESTAMP?

SYSTIMESTAMP returns current timestamp on database server, while current_timestamp returns current timestamp on client machine. So if your database server is in New York and client box is in California, SYSTIMESTAMP will be 3 hours ahead of CURRENT_TIMESTAMP.


2 Answers

This would do it:

select round((cast(current_timestamp as date) - date '1970-01-01')*24*60*60) from dual

Though I wouldn't use current_timestamp if I was only interested in seconds, I would use SYSDATE:

select round((SYSDATE - date '1970-01-01')*24*60*60) from dual
like image 122
Tony Andrews Avatar answered Sep 28 '22 07:09

Tony Andrews


Maybe not completely relevant. I had to resolve other way around problem (e.g. Oracle stores timestamp in V$RMAN_STATUS and V$RMAN_OUTPUT) and I had to convert that to date/timestamp. I was surprised, but the magic date is not 1970-01-01 there, but 1987-07-07. I looked at Oracle's history and the closest date I can think of is when they ported Oracle products to UNIX. Is this right?

Here's my SQL

SELECT /*+ rule */
         to_char(min(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON-YYYY HH24:MI:SS') start_tm
       , to_char(to_char(max(stamp)/(24*60*60) + date '1987-07-07', 'DD-MON HH24:MI:SS')) end_tm
FROM V$RMAN_STATUS 
START WITH (RECID, STAMP) =
     (SELECT MAX(session_recid),MAX(session_stamp) FROM V$RMAN_OUTPUT) 
CONNECT BY PRIOR RECID = parent_recid ;
like image 24
Tagar Avatar answered Sep 28 '22 08:09

Tagar