ACE Director Alum Daniel Morgan, founder of Morgan's Library, is scheduling
complimentary technical Workshops on Database Security for the first 30
Oracle Database customers located anywhere in North America, EMEA, LATAM, or
APAC that send an email to
asra_us@oracle.com. Request a Workshop for
your organization today.
Time Zones
List of Time Zones
set linesize 121
col tzname format a30
col tzabbrev format a30
GREATEST(pattern IN TIME_UNCONSTRAINED) RETURN TIME_UNCONSTRAINED;
GREATEST(pattern IN TIME_TZ_UNCONSTRAINED) RETURN TIME_TZ_UNCONSTRAINED;
GREATEST(pattern IN TIMESTAMP_UNCONSTRAINED) RETURN TIMESTAMP_UNCONSTRAINED;
GREATEST(pattern IN TIMESTAMP_TZ_UNCONSTRAINED) RETURN TIMESTAMP_TZ_UNCONSTRAINED;
GREATEST(pattern IN TIMESTAMP_LTZ_UNCONSTRAINED) RETURN TIMESTAMP_LTZ_UNCONSTRAINED;
LEAST(pattern IN TIME_UNCONSTRAINED) RETURN TIME_UNCONSTRAINED;
LEAST(pattern IN TIME_TZ_UNCONSTRAINED) RETURN TIME_TZ_UNCONSTRAINED;
LEAST(pattern IN TIMESTAMP_UNCONSTRAINED) RETURN TIMESTAMP_UNCONSTRAINED;
LEAST(pattern IN TIMESTAMP_TZ_UNCONSTRAINED) RETURN TIMESTAMP_TZ_UNCONSTRAINED;
LEAST(pattern IN TIMESTAMP_LTZ_UNCONSTRAINED) RETURN TIMESTAMP_LTZ_UNCONSTRAINED;
Current date and time in the session time zone in a value of data type TIMESTAMP.
The difference between this function and CURRENT_TIMESTAMP is that LOCALTIMESTAMP returns a TIMESTAMP value while CURRENT_TIMESTAMP returns a TIMESTAMP WITH TIME ZONE value
LOCALTIMESTAMP RETURN TIMESTAMP_UNCONSTRAINED;
ALTER SESSION SET TIME_ZONE = '-5:00';
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM dual;
ALTER SESSION SET TIME_ZONE = '-8:00';
SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM dual;
CREATE TABLE local_test (col1 TIMESTAMP WITH LOCAL TIME ZONE);
-- the following statement fails because the mask does not include the TIME ZONE portion of the return type of the function
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, 'DD-MON-RR HH.MI.SSXFF'));
-- the following statement uses the correct format mask to match the return type of LOCALTIMESTAMP
INSERT INTO local_test VALUES
(TO_TIMESTAMP(LOCALTIMESTAMP, 'DD-MON-RR HH.MI.SSXFF PM'));
CREATE TABLE ts_test (
x TIMESTAMP,
y TIMESTAMP(0),
z TIMESTAMP(9));
desc ts_test
INSERT INTO ts_test
(x, y, z)
VALUES
(timestamp'2021-08-08 09:00:00.123456789',
timestamp'2021-08-08 09:00:00.123456789',
timestamp'2021-08-08 09:00:00.123456789');
set linesize 121
col x format a30
col y format a21
col z format a31
SELECT * FROM ts_test;
INSERT INTO ts_test
(x, y, z)
VALUES
(localtimestamp, localtimestamp, localtimestamp);
SELECT * FROM ts_test;
SELECT VSIZE(x), VSIZE(y), VSIZE(z)
FROM ts_test;
Table With Time Zone
CREATE TABLE tswtz_test (
msg VARCHAR2(40),
x TIMESTAMP WITH TIME ZONE);
desc tswtz_test
ALTER SESSION SET TIME_ZONE = '-6:00';
col x foramt a35
INSERT INTO tswtz_test
(msg, x)
VALUES
('literal TS without TZ', timestamp'2021-08-08 09:00:00.123456789');
INSERT INTO tswtz_test
(msg, x)
VALUES
('SysTimeStamp (has TZ from DB)', systimestamp);
INSERT INTO tswtz_test
(msg, x)
VALUES
('LocalTimeStamp (has NO TZ)', LocalTimeStamp);
INSERT INTO tswtz_test
(msg, x)
VALUES
('Current_Timestamp (has TZ from client)', Current_Timestamp);
SELECT * FROM tswtz_test;
/* Lastly, notice the behavior of CURRENT_DATE and SYSDATE Current_Date is a lot like SYSDATE but is timezone sensitive. */
SELECT TO_CHAR(CURRENT_DATE, 'dd-mon-yyyy hh24:mi:ss'),
TO_CHAR(sysdate, 'dd-mon-yyyy hh24:mi:ss')
FROM dual;
ALTER SESSION SET time_zone = local;
SELECT TO_CHAR(CURRENT_DATE, 'dd-mon-yyyy hh24:mi:ss'),
TO_CHAR(sysdate, 'dd-mon-yyyy hh24:mi:ss')
FROM dual;
Extract Timestamp Components
col TR format a10
SELECT
EXTRACT(year FROM systimestamp) EY,
EXTRACT(month FROM systimestamp) EM,
EXTRACT(day FROM systimestamp) ED,
EXTRACT(hour FROM systimestamp) EH,
EXTRACT(minute FROM systimestamp) EM,
EXTRACT(second FROM systimestamp) ES,
EXTRACT(timezone_hour FROM systimestamp) TH,
EXTRACT(timezone_minute FROM systimestamp) TM,
EXTRACT(timezone_region FROM systimestamp) TR,
EXTRACT(timezone_abbr FROM systimestamp) TA
FROM dual;
Extract Current Timestamp Components
col TR format a10
SELECT
EXTRACT(year FROM current_timestamp) EY,
EXTRACT(month FROM current_timestamp) EM,
EXTRACT(day FROM current_timestamp) ED,
EXTRACT(hour FROM current_timestamp) EH,
EXTRACT(minute FROM current_timestamp) EM,
EXTRACT(second FROM current_timestamp) ES,
EXTRACT(timezone_hour FROM current_timestamp) TH,
EXTRACT(timezone_minute FROM current_timestamp) TM,
EXTRACT(timezone_region FROM current_timestamp) TR,
EXTRACT(timezone_abbr FROM current_timestamp) TA
FROM dual;
Extract Current Timestamp Components after altering the time zone
col TR format a10
ALTER SESSION SET time_zone = 'US/Eastern';
SELECT
EXTRACT(year FROM current_timestamp) EY,
EXTRACT(month FROM current_timestamp) EM,
EXTRACT(day FROM current_timestamp) ED,
EXTRACT(hour FROM current_timestamp) EH,
EXTRACT(minute FROM current_timestamp) EM,
EXTRACT(second FROM current_timestamp) ES,
EXTRACT(timezone_hour FROM current_timestamp) TH,
EXTRACT(timezone_minute FROM current_timestamp) TM,
EXTRACT(timezone_region FROM current_timestamp) TR,
EXTRACT(timezone_abbr FROM current_timestamp ) TA
FROM dual;
set serveroutput on
DECLARE
t TIMESTAMP WITH TIME ZONE:=timestamp'2021-01-01 01:01:01 US/Pacific';
BEGIN
dbms_output.put_line(extract(timezone_abbr from t));
dbms_output.put_line(extract(timezone_region from t));
END;
/
ALTER SESSION SET time_zone = local;
Time Math
Time Math Demo
SELECT current_timestamp + INTERVAL '5' year(1)
FROM dual;
SELECT current_timestamp + INTERVAL '10:30' MINUTE TO SECOND
FROM dual;
CURRENT_TIMESTAMP+INTERVAL'10:30'MINUTETOSECOND
------------------------------------------------
31-OCT-21 08.50.13.601000000 AM -05:00
-- this will fail ... there is no Feb. 29th in 2021
SELECT timestamp'2021-02-29 00:00:00' + INTERVAL '1' year(1)
FROM dual;
*
ERROR at line 1:
ORA-01847: day of month must be between 1 and last day of month
SELECT timestamp'2021-02-28 00:00:00' + INTERVAL '1' year(1)
FROM dual;
TIMESTAMP'2021-02-2800:00:00'+INTERVAL'1'YEAR(1)
-------------------------------------------------
28-FEB-22 12.00.00.000000000 AM
SELECT add_months(timestamp'2021-02-28 00:00:00',12)
FROM dual;