Posts Tagged ‘milliseconds’
Convert from milliseconds since epoch to Oracle timestamp
Today I needed to convert from numbers in a log file originally generated using System.currentTimeMillis()
in Java to Oracle timestamps and I wanted to do it with PL/SQL, without involving Java in the database. I love Java, but inside Oracle? No thanks.
Surely this is simple and surely the Internet abounds with examples? No and no. It is easy to create a date, but date columns loose precision. I needed to preserve the milliseconds. There are many examples for getting the number of milliseconds since the epoch from an Oracle timestamp, but not for creating a timestamp from a millisecond value.
After some thinking I ended up with the following code:
create or replace function millis_to_timestamp(p_millis in number)
return timestamp is
v_datePart date;
v_millisPart number;
begin
v_datePart := to_date('19700101', 'YYYYMMDD') + (p_millis / 86400000);
v_millisPart := mod(p_millis, 1000);
return to_timestamp(to_char(v_datePart, 'YYYYMMDDHH24MISS') ||
substr(to_char(v_millisPart + 1000), 2, 3), 'YYYYMMDDHH24MISSFF');
end;
Not that pretty, but it works! Share and enjoy…
Random Posts
Tag Cloud
ActiveMQ AKS AMQ AQ Azure Biztalk clob copy-of developer conference EAP6 Eclipse encryption expired password ffmpeg Java Java EE JAX-WS JBoss JDBC JMS JPA JSF Kubernetes load testing Logback Maven milliseconds namespaces netty Oracle Oracle performance tuning Performance port range rotate Security SOA Suite SQL transpose video VirtualBox vmware WebSphere Windows 2008 server XA XSLTCategories
Blogroll
Archives
- March 2023
- August 2020
- July 2020
- January 2020
- May 2019
- March 2019
- June 2018
- May 2018
- April 2018
- January 2018
- December 2017
- November 2017
- October 2017
- June 2017
- May 2017
- March 2017
- November 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- December 2015
- October 2015
- September 2015
- August 2015
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- November 2014
- September 2014
- August 2014
- July 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- September 2013
- August 2013
- July 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- October 2012
- September 2012
- August 2012
- June 2012
- April 2012
- March 2012
- February 2012
- November 2011
- October 2011
- August 2011
- December 2010
- October 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010