Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


First millisecond and Last millisecond in a day

Posted on 2013-01-30
Medium Priority
Last Modified: 2013-02-05
How to get the very first millisecond and very last millisecond in a day in Oracle?

Question by:sath350163
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 29

Expert Comment

ID: 38837449
You could use timestamp:


Open in new window


Author Comment

ID: 38837461
Thanks for the response.

I eseentially need to retrieve records from the very first millisecond in a day (say today) until vers last millisecond of today.
Can you please tell me how that can be achieved?
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 1500 total points
ID: 38837482
select * from table where timestamp_column >= trunc(systimestamp) and < trunc(systimestamp+1);

You could use sysdate in place of systimestamp since trunc strips the time portion of either.
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks


Author Comment

ID: 38837504

My table stores the date time as Number which is in milliseconds.
So timestamp_column equivalent column in my schema is a NUMBER column.

Here is a sample Number value representing a Date Time: 1359651194334

select * from table where MilliSecs_Number_column >= trunc(systimestamp) and < trunc(systimestamp+1);

So,  how do I convert the MilliSecs_Number_column to DATE with Timestamp in the SELECT sql?
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 1500 total points
ID: 38837605
>>Here is a sample Number value representing a Date Time: 1359651194334

What date/time does that represent?

If unix epoch, there are a TON of links out there on converting these to Oracle:

For example:

select * from table where TO_DATE('1970-01-01', 'YYYY-MM-DD') + MilliSecs_Number_column/ 86400000 >=
trunc(systimestamp) and TO_DATE('1970-01-01', 'YYYY-MM-DD') + MilliSecs_Number_column/ 86400000 < trunc(systimestamp+1);

Author Comment

ID: 38837670
Not sure. It is being stamped by JAVA code.

On converting the Number in Milliseconds to Date, I get 1/31/2013 4:53:14 PM.
SELECT TO_DATE('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS') + (1359651194334  /1000/60/60/24) from dual;

Open in new window

Your approach works. Thanks.

In the mean time, I also found another approach which is essentially giving me the same result:
select * 
  from table 
 where TO_DATE('1970-01-01', 'YYYY-MM-DD') + (1359651194334  /1000/60/60/24) >= trunc(systimestamp) 
   and TO_DATE('1970-01-01', 'YYYY-MM-DD') + (1359651194334  /1000/60/60/24) < trunc(systimestamp+1);  

Open in new window

What do you think about this approach?
LVL 77

Accepted Solution

slightwv (䄆 Netminder) earned 1500 total points
ID: 38837748
>>What do you think about this approach?

Isn't that what I posted?

1000*60*60*24 = 86400000

As for what I think:  If what is stored as a number converts to the correct date using the formula, I think it is a great idea.

You need to get with the developers to confirm what they are storing and what it resolves to.

If 1359651194334  doesn't mean 1/31/2013 4:53:14 PM, then this is a horrible idea since the conversion is wrong.

Basically you need to get from the developers or someone the date they start counting milliseconds from.  Then use that to seed the calculation.

If I was to bet money on it, I would guess unix epoch, 01/01/1970.

Author Comment

ID: 38840811
Good point. I'll have to discuss with the developers on that.

Author Comment

ID: 38840830
Thanks to both slighwv and MikeOM_DBA for the help.
LVL 49

Assisted Solution

PortletPaul earned 500 total points
ID: 38848377
java does use 1st Jan 1970

I suggest converting the date boundaries into a number that can be directly compared to the java timestamp data in your table, like this:

from table1
where (
      /* for "yesterday" */
      table1.java_timestamp >= ((truc(sysdate-1) - to_date('1970-01-01', 'YYYY-MM-DD')) * 86400000)
      table1.java_timestamp < ((truc(sysdate) - to_date('1970-01-01', 'YYYY-MM-DD')) * 86400000)

Open in new window


Author Comment

ID: 38856420
Awesome. Thanks for the findings.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question