I want to be able to store the current time in milliseconds in an Oracle number field. How do I do this via a query?
select systimestamp from dual;
returns the actual timestamp. Is there anyway that I can convert this into milliseconds the same way Java's System.currentTimeMillis()
does?
this link helps for all languages currentmillis.com for oracle:
The Java function returns the number of milliseconds which have elapsed since a fixed moment in time. That time is midnight on the first day of 1970 UTC, i.e. the start of Unix clock time.
The following function does the same for PL/SQL. It subtracts the current timestamp from the starting point (where ms=1). It extracts the various time components and turns them into seconds. Finally it multiplies everything by 1000 to get the value in milliseconds:
If you have Java enabled in the database you may find it simpler to create a Java Stored Procedure instead:
Comparison of the two approaches:
(My thanks go to Simon Nickerson, who spotted the typo in the previous version of my PL/SQL function which produced an anomalous result.)
Incidentally, if you are only interested in time to the nearest centisecond, Oracle has a built-in for that: DBMS_UTILITY.GET_TIME().
Try this -
select extract(day from(sys_extract_utc(systimestamp AT TIME ZONE 'GMT') - to_timestamp('1970-01-01', 'YYYY-MM-DD'))) * 86400000 + to_number(to_char(sys_extract_utc(systimestamp AT TIME ZONE 'GMT'), 'SSSSSFF3')) from dual;