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.
Recommended tutorial: "Oracle Video Tutorial"
查询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('2021-01-02 23:59:59', 'yyyy-mm-dd hh24:mi:ss');
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('2021-01-01', 'yyyy-mm-dd') AND t.s_ra_time <= to_date('2021-01-02', 'yyyy-mm-dd'); --to_date('2021-01-02', 'yyyy-mm-dd') = 2021-01-02 00:00:00 超过2号0点属于2号的数据不会显示
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; --结果:2022-01-26 13:04:53 select to_char(sysdate,'yyyy') as nowYear from dual; --结果:2022 select to_char(sysdate,'mm') as nowMonth from dual; --结果:01 select to_char(sysdate,'dd') as nowDay from dual; --结果:26 select to_char(sysdate,'hh24') as nowHour from dual; --结果:13 select to_char(sysdate,'mi') as nowMinute from dual; --结果:04 select to_char(sysdate,'ss') as nowSecond from dual; --结果:53 select to_date('2022-01-26 13:04:53','yyyy-mm-dd hh24:mi:ss') from dual
select to_char(to_date('2022-01-26','yyyy-mm-dd'),'day') from dual; --结果:星期三
select floor(sysdate - to_date('20220101','yyyymmdd')) from dual;
select 1, TO_DATE(null) from dual;
select months_between(to_date('12-31-2021','MM-DD-YYYY'),to_date('01-31-2021','MM-DD-YYYY')) "MONTHS" FROM DUAL; --结果:11
NEXT_DAY(date,char) select next_day(sysdate,2) from dual; --当前时间的下一个周一 --1表示星期日,2代表星期一
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual; --闰年的处理方法 to_char( last_day( to_date('02'|| :year,'mmyyyy') ), 'dd') --如果是28就不是闰年
select TO_CHAR(SYSDATE,'DDD'),sysdate from dual;
trunc[truncated to the nearest date, in days], the returned Date type
select sysdate S1, trunc(sysdate) S2, //返回当前日期,无时分秒 trunc(sysdate,'year') YEAR, //返回当前年的1月1日,无时分秒 trunc(sysdate,'month') MONTH , //返回当前月的1日,无时分秒 trunc(sysdate,'day') DAY //返回当前星期的星期天,无时分秒 from dual
select greatest('2021-01-04','2022-01-04','2019-02-04') from dual; --结果:2022-01-04
select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))/365) as spanYears from dual //时间差-年 select ceil(months_between(sysdate,to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanMonths from dual //时间差-月 select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))) as spanDays from dual //时间差-天 select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24) as spanHours from dual //时间差-时 select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60) as spanMinutes from dual //时间差-分 select floor(to_number(sysdate-to_date('2020-11-02 15:55:03','yyyy-mm-dd hh24:mi:ss'))*24*60*60) as spanSeconds from dual //时间差-秒
SELECT Trunc(Trunc(SYSDATE, 'MONTH') - 1, 'MONTH') First_Day_Last_Month, --最后一月最后一天 Trunc(SYSDATE, 'MONTH') - 1 / 86400 Last_Day_Last_Month, --最后一月最后一天 Trunc(SYSDATE, 'MONTH') First_Day_Cur_Month, --当前月第一天 LAST_DAY(Trunc(SYSDATE, 'MONTH')) + 1 - 1 / 86400 Last_Day_Cur_Month --当前月最后一天 FROM dual;
当前时间减去7分钟的时间 select sysdate,sysdate - interval '7' MINUTE from dual 当前时间减去7小时的时间 select sysdate - interval '7' hour from dual 当前时间减去7天的时间 select sysdate - interval '7' day from dual 当前时间减去7月的时间 select sysdate,sysdate - interval '7' month from dual 当前时间减去7年的时间 select sysdate,sysdate - interval '7' year from dual 时间间隔乘以一个数字(也就是8个小时*2倍,16个小时之前的数据) select sysdate,sysdate - 8 *interval '2' 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!