当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

全新荣威RX5,27寸大屏吸引人,安全、舒适一个不落

云原生应用平台的核心模块有哪些

yolov7 innovation point

iNFTnews | Seeing the two sides of the metaverse, what is the true Internet and the Internet of value?

DVWA Clearance Log 2 - Command Injection

yolov7创新点

零代码工具推荐---HiFlow

如何搭建威纶通触摸屏与S7-200smart之间无线PPI通信?

Rear tube implements breadcrumb function

超赞!发现一个APP逆向神器!
随机推荐
Hongxing, donate another million
4年手工测试被应届生取代了,用血与泪的教训给xdm一个忠告,该学自动化了...
win10打印服务无法启动(运行时错误automation)
Use compilation to realize special effects of love
LayaBox---TypeScript---Iterator and generator
周杰伦新歌发布,爬取《Mojito》MV弹幕,看看粉丝们都说的些啥!
The R language uses the ggtexttable function of the ggpubr package to visualize the table data (draw the table directly or add the table data to the image), set the theme parameter to customize the fi
logo 图标(php图片加文字水印)
2022.7.25-7.31 AI行业周刊(第108期):值钱比赚钱更重要
R language ggplot2 visualization: use the ggtexttable function of the ggpubr package to visualize tabular data (directly draw tabular graphs or add tabular data to images), use tbody_add_border to add
R语言ggpubr包的ggline函数可视化分组折线图、add参数为mean_se和dotplot可视化不同水平均值的折线图并为折线图添加误差线(se标准误差)和点阵图、自定义palette设置颜色
软件测试之发现和解决bug
LayaBox---TypeScript---高级类型
php组件漏洞
currentstyle 织梦_dede currentstyle属性完美解决方案
games202:三,实时环境光照IBL + PRT
38岁女儿不恋爱没有稳定工作老母亲愁哭
sqlmap安装教程用w+r打开(sqlyog安装步骤)
R语言ggplot2可视化:基于aes函数中的fill参数和shape参数自定义绘制分组折线图并添加数据点(散点)、使用theme函数的legend.position函数配置图例到图像右侧
How to choose a truly "easy-to-use, high-performance" remote control software