Oracle - Fetch date/time in milliseconds from DATE

2019-02-18 02:57发布

问题:

I have last_update_date column defined as DATE field

I want to get time in milliseconds.

Currently I have:

TO_CHAR(last_update_date,'YYYY-DD-MM hh:mi:ss am')

But I want to get milliseconds as well.

I googled a bit and think DATE fields will not have milliseconds. only TIMESTAMP fields will.

Is there any way to get milliseconds? I do not have option to change data type for the field.

回答1:

DATE fields on Oracle only store the data down to a second so there is no way to provide anything more precise than that. If you want more precision, you must use another type such as TIMESTAMP.

Here is a link to another SO question regarding Oracle date and time precision.



回答2:

As RC says, the DATE type only supports a granularity down to the second.

If converting to TIMESTAMP is truly not an option then how about the addition of another numerical column that just holds the milliseconds?

This option would be more cumbersome to deal with than a TIMESTAMP column but it could be workable if converting the type is not possible.



回答3:

In a similar situation where I couldn't change the fields in a table, (Couldn't afford to 'break' third party software,) but needed sub-second precision, I added a 1:1 supplemental table, and an after insert trigger on the original table to post the timestamp into the supplemental table.

If you only need to know the ORDER of records being added within the same second, you could do the same thing, only using a sequence as a data source for the supplemental field.