datetime function
SELECT DBTIOMEZONE FROM DUAL; view database time zone
SELECT SYSTIMSTAMP FROM DUAL; view local timezone
SELECT SESSIONTIMEZONE FROM DUAL; view session time zone
TIMES TAMP has no concept of time zone, what time is stored in, and what time is taken out
TIMES TAMP The time and time zone are all stored, and the originally stored time and time zone can be obtained when fetching data
TIMES TAMP WITH LOCAL TIME ZONE converts the input time according to the time zone of the database timezone and stores the time, and returns the data to the user according to the time zone of the user SESSION when the data is retrieved, and the retrieved time does not have the time zone format
View three timestamp formats
CREATE TABLE T31(
TIMEDATE DATE,
TIMEDATE2 TIMESTAMP,
TIMEDATE3 TIMESTAMP WITH TIME ZONE,
TIMEDATE4 TIMESTAMP WITH LOCAL TIME ZONE
);
INSERT INTO T31 VALUES(SYSDATE,SYSDATE,SYSDATE,SYSDATE);
SELECT * FROM T31;
SELECT SESSIONTIMEZONE,CURRENT_DATE FROM DUAL;--current time
SELECT SESSIONTIMEZONE,CURRENT_TIMESTAMP FROM DUAL;--current timestamp
SELECT SESSIONTIMEZONE,LOCALTIMESTAMP FROM DUAL;--local timestamp
ALTER SESSION
SET NLS_DATE_FORMAT = ‘DD-MON-YYYY HH24:MI:SS’;
ALTER SESSION SET TIME_ZONE='-5:00'; change session time zone
ALTER TABLE EMPLOYEES
MODIFY HIRE_DATE TIMESTAMP;--change HIRE_DATE to timestamp format
SELECT HIRE_DATE
FROM EMPLOYEES;
CREATE TABLE WEB_ORDERS
(ORDER_DATE TIMESTAMP WITH TIME ZONE,DELIVERY_TIME TIMESTAMP WITH LOCAL TIME ZONE);
--Create a Time zone type field
--Create a local time zone type field
ALTER SESSION SET TIME_ZONE=’-6:00’;
INSERT INTO WEB_ORDERS VALUES -- insert time data
(SYSDATE,SYSDATE);
SELECT * FROM WEB_ORDERS; -- view data
SELECT T.EMPNO,T.ENAME
(SYSDATE-T.HIREDATE) YEAR TO MONTH AS HIRE_YEAR
FROM SCOTT.EMP T; -- Display the entry year in the format of how many years and how many months
INTERVAL usage
CREATE TABLE WARRANTY
(PROD_ID NUMBER,WARRANTY_TIME INTERVAL YEAR(3) TO MONTH);
INSERT INTO WARRANTY VALUES(123,INTERVAL’9’MONTH)
SELECT * FROM WARRANTY;
The function TZ_OFFSET displays the time zone
SELECT TZ_OFFSET('US/Eastern') FROM DUAL to view the time zone
SELECT TZNAME,TZABBREV,TZ_OFFSET(TZNAME) FROM V$TIMEZONE_NAMES;--Query view world time zone name and time zone
EXTRACT extraction
Display the year from SYSDATE.
SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL;--Extract (display only) year
Display the month of HIRE_DATE for employee with MANAGER_ID 100
SELECT LAST_NAME,HIRE_DATE,
EXTRACT(MONTH FROM HIRE_DATE)--extract the month
FROM EMPLOYEES
WHERE MANAGER_ID=100;
FROM_TZ
Show TIMESTAMP value '2000-03-28 08:00:00' time zone is 'Australia/North' (Australia/North), TIMESTAMP WITH TIME ZONE value
SELECT FROM_TZ(TIMESTAMP ‘2000-07-12 08:00:00’,’Austrailia/North’)FROM DUAL;
TO_TIMESTAMP
Displays the TIMESTAMP value for the string '2007-03-06 11:00:00'
SELECT TO_TIMESTAMP (‘2007-03-06 11:00:00’,’YYYY-MM-DD HH:MI:SS’)FROM DUAL;
TO_YMINTERVAL
Displays the date one year and two months after the hire date of the employee with DEPARTMENT_ID 20
SELECT HIRE_DATE,
HIRE_DATE+TO_YMINTERVAL(‘01-02’) AS
HIRE_DATE_YMINTERVAL
FROM EMPLOYEES
WHERE DEPARTMENT_ID=20;