Home  >  Article  >  Database  >  Summary of some common situations of Oracle query based on time

Summary of some common situations of Oracle query based on time

WBOY
WBOYforward
2022-09-02 17:04:522551browse

This article brings you relevant knowledge about Oracle. Querying based on time is a function we often encounter in daily development. Here are some common queries about Oracle based on time. The situation is introduced in detail through example code in the article. Friends in need can refer to it.

Summary of some common situations of Oracle query based on time

Recommended tutorial: "Oracle Video Tutorial"

1. Query data within a time period

查询2021-01-01  至  2021-01- 02 的数据
SELECT *
FROM t_table1 t
WHERE t.d_time >= to_date('2021-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
	AND t.d_time <= to_date(&#39;2021-01-02 23:59:59&#39;, &#39;yyyy-mm-dd hh24:mi:ss&#39;);

The following SQL will only query data from 2021-01-01 to 2021-1-2 00:00:00

SELECT *
FROM T_EVENT_MANAGEMENT t
WHERE t.s_ra_time >= to_date(&#39;2021-01-01&#39;, &#39;yyyy-mm-dd&#39;)
	AND t.s_ra_time <= to_date(&#39;2021-01-02&#39;, &#39;yyyy-mm-dd&#39;);
--to_date(&#39;2021-01-02&#39;, &#39;yyyy-mm-dd&#39;) = 2021-01-02 00:00:00 超过2号0点属于2号的数据不会显示

2. Date and character conversion function usage (to_date,to_char)

select to_char(sysdate,&#39;yyyy-mm-dd hh24:mi:ss&#39;) as nowTime from dual;  --结果:2022-01-26 13:04:53
select to_char(sysdate,&#39;yyyy&#39;) as nowYear   from dual; --结果:2022
select to_char(sysdate,&#39;mm&#39;)   as nowMonth  from dual; --结果:01
select to_char(sysdate,&#39;dd&#39;)   as nowDay    from dual; --结果:26
select to_char(sysdate,&#39;hh24&#39;) as nowHour   from dual; --结果:13
select to_char(sysdate,&#39;mi&#39;)   as nowMinute from dual; --结果:04
select to_char(sysdate,&#39;ss&#39;)   as nowSecond from dual; --结果:53


select to_date(&#39;2022-01-26 13:04:53&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;)  from dual

3. Query the day of the week for a certain day

select to_char(to_date(&#39;2022-01-26&#39;,&#39;yyyy-mm-dd&#39;),&#39;day&#39;) from dual;   --结果:星期三

4. The direct difference in days between two dates

select floor(sysdate - to_date(&#39;20220101&#39;,&#39;yyyymmdd&#39;)) from dual;

5. Query an empty time type

select 1, TO_DATE(null) from dual;

6. Used to calculate the number of months between date1 and date2

select months_between(to_date(&#39;12-31-2021&#39;,&#39;MM-DD-YYYY&#39;),to_date(&#39;01-31-2021&#39;,&#39;MM-DD-YYYY&#39;)) "MONTHS" FROM DUAL; --结果:11

7. The date of the next day of the week (specified by char) at the specified time,

NEXT_DAY(date,char) 

select   next_day(sysdate,2) from dual;  --当前时间的下一个周一
--1表示星期日,2代表星期一

8. Get the number of days in this year

select add_months(trunc(sysdate,&#39;year&#39;), 12) - trunc(sysdate,&#39;year&#39;) from dual;
 --闰年的处理方法     
to_char( last_day( to_date(&#39;02&#39;|| :year,&#39;mmyyyy&#39;) ), &#39;dd&#39;)     
 --如果是28就不是闰年

9. Get the number of days in this year that the current time is

 select TO_CHAR(SYSDATE,&#39;DDD&#39;),sysdate from dual;

trunc[truncated to the nearest date, in days], the returned Date type

 select sysdate S1,                    
     trunc(sysdate) S2,                 //返回当前日期,无时分秒
     trunc(sysdate,&#39;year&#39;) YEAR,        //返回当前年的1月1日,无时分秒
     trunc(sysdate,&#39;month&#39;) MONTH ,     //返回当前月的1日,无时分秒
     trunc(sysdate,&#39;day&#39;) DAY           //返回当前星期的星期天,无时分秒
   from dual

10. Return the latest date in the date list

select greatest(&#39;2021-01-04&#39;,&#39;2022-01-04&#39;,&#39;2019-02-04&#39;) from dual;  --结果:2022-01-04

11. Calculate the time difference

 select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))/365) as spanYears from dual        //时间差-年
select ceil(months_between(sysdate,to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))) as spanMonths from dual        //时间差-月
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))) as spanDays from dual             //时间差-天
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24) as spanHours from dual         //时间差-时
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24*60) as spanMinutes from dual    //时间差-分
select floor(to_number(sysdate-to_date(&#39;2020-11-02 15:55:03&#39;,&#39;yyyy-mm-dd hh24:mi:ss&#39;))*24*60*60) as spanSeconds from dual //时间差-秒

12. Find the first and last day of the month

 SELECT Trunc(Trunc(SYSDATE, &#39;MONTH&#39;) - 1, &#39;MONTH&#39;) First_Day_Last_Month,  --最后一月最后一天
       Trunc(SYSDATE, &#39;MONTH&#39;) - 1 / 86400 Last_Day_Last_Month,            --最后一月最后一天
       Trunc(SYSDATE, &#39;MONTH&#39;) First_Day_Cur_Month,                        --当前月第一天
       LAST_DAY(Trunc(SYSDATE, &#39;MONTH&#39;)) + 1 - 1 / 86400 Last_Day_Cur_Month --当前月最后一天
   FROM dual;

13. Query the time before the time (replace the minus sign with a plus sign after checking)

当前时间减去7分钟的时间
select sysdate,sysdate - interval &#39;7&#39; MINUTE from dual

当前时间减去7小时的时间
select sysdate - interval &#39;7&#39; hour from dual

当前时间减去7天的时间
select sysdate - interval &#39;7&#39; day from dual

当前时间减去7月的时间
select sysdate,sysdate - interval &#39;7&#39; month from dual

当前时间减去7年的时间
select sysdate,sysdate - interval &#39;7&#39; year from dual

时间间隔乘以一个数字(也就是8个小时*2倍,16个小时之前的数据)
select sysdate,sysdate - 8 *interval &#39;2&#39; hour from dual

 
获取七天之后的时间
select (sysdate + 7) from dual;

获取前一个月的时间(正数时是加月,负数时为减月)
select add_months(sysdate,-1) from dual;

select sysdate+1 from dual 加一天
select sysdate+1/24 from dual 加1小时
select sysdate+1/(24*60) from dual 加1分钟
select sysdate+1/(24*60*60) from dual 加1秒钟

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of Summary of some common situations of Oracle query based on time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete