How to extract millisecond from date in Oracle?
30,717
i have full date (with time)
This can only be done using a timestamp
. Although Oracle's date
does contain a time, it only stores seconds, not milliseconds.
To get the fractional seconds from a timestamp use to_char()
and convert that to a number:
select to_number(to_char(timestamp '2016-03-16 10:45:04.252', 'FF3'))
from dual;
Comments
-
Nikunj Chavda over 3 years
i have full date(with time). But i want only millisecond from date.
please tell me one line solution
for example:
date= 2016/03/16 10:45:04.252
i want this answer=252
i try to use this query.
SELECT ADD_MONTHS(millisecond, -datepart('2016/03/16 10:45:04.252', millisecond), '2016/03/16 10:45:04.252') FROM DUAL;
but i'm not success.
-
John Doe over 4 yearscode only answers, while being correct, are not really useful to understand the solution. Please consider adding some text explaining what you have done and what the code means
-
Nikunj Chavda over 4 yearsThanks you for efforts.i got my answer
-
Seymour about 4 yearsThis is the cleanest answer.
-
Dai about 3 yearsWhat about
EXTRACT( timestamp_value, MILLISECOND )
? (EDIT: Ah, that's only in "Oracle Big Data", not Oracle Database, even in version 21c, that's crazy...) -
Dai about 3 yearsI noticed that this works in Oracle 11g:
SELECT ( timestamp_value - CAST( ValidFrom AS timestamp(0) ) ) AS ms FROM tableName
however the type ofms
is stilltimestamp(n)
.