Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract time part from TimeStamp column in ORACLE

Tags:

sql

oracle

Currently I'm using MyTimeStampField-TRUNC(MyTimeStampField) to extract the time part from a timestamp column in Oracle.

SELECT CURRENT_TIMESTAMP-TRUNC(CURRENT_TIMESTAMP) FROM DUAL

This returns

+00 13:12:07.100729

This works OK for me, to extract the time part from a timestamp field, but I'm wondering if there is a better way (may be using a built-in function of ORACLE) to do this?

like image 941
RRUZ Avatar asked Jun 01 '10 17:06

RRUZ


People also ask

What is Extract time?

EXTRACTTIMES() Extract list of Time values within textual value. Returns a list of Time values within the textual value. Includes specific times, times written in prose, and relative times, such as “tomorrow at 3pm”, “one hour from now”, and so on.

Can you use Datepart in Oracle?

However, datepart() function works in SQL Server, Oracle, and Azure SQL databases only. For other database management servers such as PostgreSQL and MYSQL, we can use functions like EXTRACT().

What is Trunc in Oracle?

Purpose. The TRUNC (number) function returns n1 truncated to n2 decimal places. If n2 is omitted, then n1 is truncated to 0 places. n2 can be negative to truncate (make zero) n2 digits left of the decimal point.


2 Answers

What about EXTRACT() function?

like image 161
Frank Heikens Avatar answered Nov 07 '22 19:11

Frank Heikens


You could always do something like:

select TO_DATE(TO_CHAR(SYSDATE,'hh24:mi:ss'),'hh24:mi:ss') from dual

I believe this will work with timestamps as well.

like image 37
LBushkin Avatar answered Nov 07 '22 19:11

LBushkin