当前位置:网站首页>Oracle根据时间查询
Oracle根据时间查询
2022-08-02 10:15:00 【免费的东西】
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秒钟
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Spearman's correlation coefficient
- 详细总结SoC、DSP、MCU、GPU和FPGA等基础概念
- 3D激光slam:LeGO-LOAM---地面点提取方法及代码分析
- The 38-year-old daughter is not in love and has no stable job, the old mother is crying
- The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
- currentstyle 织梦_dede currentstyle属性完美解决方案
- R language time series data arithmetic operation: use the log function to log the time series data, and use the diff function to calculate the successive difference of the logarithmic time series data
- 如何封装微信小程序的 wx.request() 请求
- 读博一年后对机器学习工程的思考
- 每日一题练习1-15
猜你喜欢
随机推荐
21年毕业转行软件测试,从0收入到月薪过万,我真的很幸运...
Geoffery Hinton:深度学习的下一个大事件
LayaBox---TypeScript---Iterator and generator
R language ggplot2 visualization: use the ggbarplot function of the ggpubr package to visualize the horizontal column chart (bar chart), use the orientation parameter to set the column chart to be tra
多大数量级会出现哈希碰撞
读博一年后对机器学习工程的思考
阿里CTO程立:阿里巴巴开源的历程、理念和实践
牛客网项目17节生成验证码 刷新验证码一直没反应
字节跳动软件测试岗,收到offer后我却拒绝了~给面试的人一些忠告....
The heavyweights are coming!Spoilers for the highlights of the Alibaba Cloud Life Science and Intelligent Computing Summit
Hello, my new name is "Bronze Lock/Tongsuo"
如何安装dosbox(pycharm详细安装教程)
LayaBox---TypeScript---Module
LayaBox---TypeScript---JSX
Turning and anti-climbing attack and defense
The R language uses the rollapply function in the zoo package to apply the specified function to the time series in a rolling manner and the window moves, and set the align parameter to specify that t
如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?
软件工程国考总结——选择题
第十七章 Excel操作
QT专题:自定义部件