1. 查询时间段之内的数据
查询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');
以下sql,只会查询2021-01-01至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号的数据不会显示
2. 日期和字符转换函数用法(to_date,to_char)
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
3. 查询某天星期几
select to_char(to_date('2022-01-26','yyyy-mm-dd'),'day') from dual; --结果:星期三
4. 两个日期直接相差天数
select floor(sysdate - to_date('20220101','yyyymmdd')) from dual;
5. 查询出一个空的时间类型
select 1, to_date(null) from dual;
6. 用于计算date1和date2之间有几个月
select months_between(to_date('12-31-2021','mm-dd-yyyy'),to_date('01-31-2021','mm-dd-yyyy')) "months" from dual; --结果:11
7. 指定时间的下一个星期几(由char指定)所在的日期,
next_day(date,char) select next_day(sysdate,2) from dual; --当前时间的下一个周一 --1表示星期日,2代表星期一
8. 获取今年的天数
select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual; --闰年的处理方法 to_char( last_day( to_date('02'|| :year,'mmyyyy') ), 'dd') --如果是28就不是闰年
9. 获取当前时间是今年的第多少天
select to_char(sysdate,'ddd'),sysdate from dual;
trunc[截断到最接近的日期,单位为天] ,返回的是日期类型
select sysdate s1, trunc(sysdate) s2, //返回当前日期,无时分秒 trunc(sysdate,'year') year, //返回当前年的1月1日,无时分秒 trunc(sysdate,'month') month , //返回当前月的1日,无时分秒 trunc(sysdate,'day') day //返回当前星期的星期天,无时分秒 from dual
10. 返回日期列表中最晚日期
select greatest('2021-01-04','2022-01-04','2019-02-04') from dual; --结果:2022-01-04
11. 计算时间差
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 //时间差-秒
12. 查找月的第一天,最后一天
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;
13. 查询时间之前的时间(查之后把减号换成加号)
当前时间减去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秒钟
总结
到此这篇关于oracle根据时间查询的文章就介绍到这了,更多相关oracle时间查询内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!
需要了解更多数据库技术:Oracle根据时间查询的一些常见情况汇总,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/dtteaching/1210019.html