Oracle Date Correlation

--Query the data of a certain day
select t.ID,t.LAST_TIME
		from T_ORDER t WHERE
			--AND t.LAST_TIME >= to_date('2017-07-31 00:00:00','yyyy-mm-dd hh24:mi:ss')
			t.LAST_TIME >= to_date('2017-07-31','yyyy-mm-dd') -- 2017-07-31 00:00:00
			--AND t.LAST_TIME <= to_date('2017-07-31 23:59:59','yyyy-mm-dd hh24:mi:ss')
			AND t.LAST_TIME < (to_date('2017-07-31','yyyy-mm-dd') + 1) -- 2017-08-01 00:00:00
;

-- Query data within a month
select t.ID,t.LAST_TIME
		from T_ORDER t WHERE
			--AND two.LIMIT_LAST_TIME >= to_date('2017-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
			t.LAST_TIME >= to_date('2017-07','yyyy-mm') -- 2017-07-01 00:00:00
			--AND two.LIMIT_LAST_TIME <= to_date('2017-07-31 23:59:59','yyyy-mm-dd hh24:mi:ss')
			AND t.LAST_TIME < add_months(to_date('2017-07','yyyy-mm'), 1) -- 2017-08-01 00:00:00
;

-- Query data for a certain year
select t.ID,t.LAST_TIME
		from T_ORDER t WHERE
			--AND two.LIMIT_LAST_TIME >= to_date('2017-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
			t.LAST_TIME >= to_date('2017-01','yyyy-mm') -- 2017-01-01 00:00:00
			--AND two.LIMIT_LAST_TIME <= to_date('2017-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss')
			AND t.LAST_TIME < to_date('2018-01','yyyy-mm') -- 2018-01-01 00:00:00
;

 

 

Notably:

--Only convert the year, will get the date on the 1st of the current month
select to_date('2017','yyyy') AS test from dual;--2017-07-01 00:00:00

 

 Difference between dates:

-- Subtract two dates to the difference in days: 1
select (To_date('2017-07-05 09:00:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-04 09:00:00' , 'yyyy-mm-dd hh24-mi-ss')) AS test FROM DUAL;
-- Subtract two dates to the difference in days: -0.5
select (To_date('2017-07-03 00:00:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-03 12:00:00' , 'yyyy-mm-dd hh24-mi-ss')) AS test FROM DUAL;
--hour difference between two dates: 0.02
select (To_date('2017-07-04 09:01:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-04 09:00:00' , 'yyyy-mm-dd hh24-mi-ss')) * 24 AS test FROM DUAL;
-- The difference in hours between two dates: 1. ceil(n): take the smallest integer greater than or equal to n
select ceil((To_date('2017-07-04 09:01:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-04 09:00:00' , 'yyyy-mm-dd hh24-mi-ss')) * 24) AS test FROM DUAL;
-- The hour difference between the two dates: 0. floor(n): take the largest integer less than or equal to n
select floor((To_date('2017-07-04 09:01:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-04 09:00:00' , 'yyyy-mm-dd hh24-mi-ss')) * 24) AS test FROM DUAL;
-- Minutes between two dates: 61.00
select (To_date('2017-07-04 10:01:00' , 'yyyy-mm-dd hh24-mi-ss') - To_date('2017-07-04 09:00:00' , 'yyyy-mm-dd hh24-mi-ss')) * 24 * 60 AS test FROM DUAL;

 It's worth noting: two dates cannot be added directly. Add a number to the date, and the number is the number of days.

 

 interval: (time) interval

--interval: (time) interval
select sysdate,',',interval '10' SECOND ,',',
	interval '10' MINUTE, ',',
	interval '10' HOUR ,',',
	interval '10' DAY ,',',
	interval '10' MONTH ,',',
	interval '10' YEAR  
from dual;
--2017-08-02 18:22:25, 0 0:0:10.0, 0 0:10:0.0, 0 10:0:0.0, 10 0:0:0.0, 0-10, 10-0

--The current time minus 10 minutes
select sysdate,sysdate - interval '10' minute from dual;
--2017-08-02 18:08:03	2017-08-02 17:58:03

--The current time plus 10 minutes
select sysdate,sysdate + interval '10' minute from dual;
--2017-08-02 18:08:37	2017-08-02 18:18:37

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326154922&siteId=291194637