Get Current Date/Time in Oracle Database
In Oracle Database, there are some functions which can be used to get datetime, differing in the return datatype and the time zone used. Here is a comparison table of them.
Function | Return Datatype | Value Time Zone | Value Example |
---|---|---|---|
SYSTIMESTAMP | TIMESTAMP WITH TIME ZONE | DB Server OS Time Zone | 19-AUG-17 11.47.39.357000 PM -07:00 |
CURRENT_TIMESTAMP | TIMESTAMP WITH TIME ZONE | Session/Client Time Zone | 20-AUG-17 02.47.39.357000 PM +08:00 |
LOCALTIMESTAMP | TIMESTAMP | Session/Client Time Zone | 20-AUG-17 02.47.39.357000 PM |
SYSDATE | DATE | DB Server OS Time Zone | 19-AUG-17 8/19/2017 11:47:39 PM |
CURRENT_DATE | DATE | Session/Client Time Zone | 20-AUG-17 8/20/2017 02:47:39 AM |
Note: The actual output of these functions depends on NLS_TIMESTAMP_TZ_FORMAT
, NLS_TIMESTAMP_FORMAT
, NLS_DATE_FORMAT
, NLS_TERRITORY
and TIME_ZONE
.
Example Usage:
select systimestamp, current_timestamp, localtimestamp from dual;
select sysdate, current_date from dual;
To get datetime in UTC, use sys_extract_utc function or at time zone expression:
-- Get TIMESTAMP in UTC using 'sys_extract_utc'
select sys_extract_utc(systimestamp) from dual;
-- Get DATE in UTC using 'sys_extract_utc'
select cast(sys_extract_utc(systimestamp) as date) from dual;
-- Get TIMESTAMP in UTC using 'at time zone' expression
select systimestamp at time zone '0:0' from dual;
-- Get DATE in UTC using 'at time zone' expression
select cast(systimestamp at time zone '0:0' as date) from dual;