1.确定两个日期之间的工作日天数

--确定两个日期之间的工作日天数
with x0 as
(select to_date('2018-01-01','yyyy-mm-dd') as 日期 from dual
union all
select to_date('2018-01-15','yyyy-mm-dd') as 日期 from dual ),
x1 as --日期并列显示
(select min (日期) 开始日期,max(日期) 结束日期 from x0 ),
x2 as --日期之间的天数
(select 结束日期-开始日期+1 as 天数 ,开始日期,结束日期 from x1),
x3 as --构造一个从开始日期到结束日期的日期集合 (枚举日期)
(select to_char(开始日期+level-1,'DY') as 枚举日期 from x2 connect by level <=天数)
--统计日期
select sum(case when 枚举日期 in ('sat','sun')then 0 else 1 end ) as 工作日 from x3

 2.计算一年周内各日期次数

--计算一年周内各日期次数
with x0 as
(select to_date('2018-01-01','yyyy-mm-dd') as 年初 from dual ),
x1 as
(select 年初,add_months(年初,12) as 下年年初 from x0),
x2 as
(select 年初,下年年初,下年年初-年初 as 天数 from x1),
x3 as
(select 年初+level-1 as 日期 from x2 connect by level <=天数 ),
x4 as
(select 日期,to_char(日期,'DAY') AS 星期 FROM X3) SELECT 星期,count(*) from x4 group by 星期

 3.确定当前记录和下一条记录之间的相差天数

with x0 as
(select '1'as name ,to_date('2018-01-01','yyyy-mm-dd') as 日期 from dual
union all
select '2' as name,to_date('2018-01-15','yyyy-mm-dd') as 日期 from dual
union all
select '3' as name, to_date('2018-01-26','yyyy-mm-dd') as 日期 from dual ) select
name ,日期,next_d,(next_d-日期) as 相差天数
from (
select name ,日期,
lead(日期,1,null) over (order by 日期) as next_d
from x0
)

lead 函数见下文说明:(014)每日SQL学习:oracle下lag和lead分析函数

4.日期操作

select 列,行
from (
select
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') 日期,
to_char(sysdate,'hh24') as 时,
to_char(sysdate,'mm') as 分,
to_char(sysdate,'ss') as 秒,
to_char(sysdate,'dd') as 日,
to_char(sysdate,'mm') as 月,
to_char(sysdate,'yyyy') as 年,
to_char(sysdate,'ddd') as 年内第几天,
to_char(trunc(sysdate,'day'),'yyyy-mm-dd') as 周初,
to_char(trunc(sysdate,'mm'),'yyyy-mm-dd') as 月初,
to_char(last_day(sysdate),'yyyy-mm-dd') as 月末,
to_char(add_months(trunc(sysdate,'mm'),1),'yyyy-mm-dd') as 下月初,
to_char(trunc(sysdate,'yy'),'yyyy-mm-dd') as 年初,
to_char(sysdate,'day') as 周几,
to_char(sysdate,'month') as 月份
from dual
)
unpivot( 行 for 列 in (日期,时,分,秒,日,月,年,年内第几天,周初,月初,月末,下月初,年初,周几,月份 ))

结果:

说明:注意此处last_day用法,该函数返回的时分秒和参数中的一样。既:如果参数日期为 2018-03-03 12:25:34 那么last_day(参数)结果是:2018-03-31 12:25:34

用此函数做区间条件时要小心数据被过滤了。一个月的数据最好采用add_months(trunc(sysdate,'mm'),1)为区间条件。

5.时间间隔(interval)

select
interval '50' month as "month",
interval '99' day as "day",
interval '80' hour as "hour",
interval '90' minute as "minute",
interval '3.99' second as "second",
interval '123-3' year(3) to month as "year to month"
from dual

结果:

month      day                 hour                 minute             second                        year to month
      +04-02     +99 00:00:00    +03 08:00:00    +00 01:30:00    +00 00:00:03.990000    +123-03
6.提取timestamp年月日函数(extract)

select systimestamp ,
extract(year from systimestamp) as "year",
extract(month from systimestamp) as "month",
extract(day from systimestamp) as"day",
extract(hour from systimestamp) as "hour",
extract(minute from systimestamp) as "minute",
extract(second from systimestamp) as "second"
from dual

说明:

1.extract不能提取date类型中的时分秒。

2.extract可以提取interval中的信息,而to_char()不行。

3.timestamp与date类型的区别:

1.比date类型更为精确,秒后面加了.0000

2.求日期差时 date类型的差值是天数,timestamp的差值的具体到时分秒。例如:

select to_date('2018-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-sysdate from dual;
结果:148.422858796296
select to_timestamp('2018-7-28 03:12:00','yyyy-mm-dd hh24:mi:ss')-systimestamp from dual
结果:+000000148 10:08:54.229000000

7.周的计算

with x0 as
(select trunc(sysdate)-trunc(sysdate,'yy') as d from dual ),
x1 as
( select trunc(trunc(sysdate,'yy')+level-1) as d1 from x0 connect by level<=d)
select d1,to_char(d1,'d') as dd,
to_char(d1,'day') as 星期,
next_day(d1,1) as 下个周日,
to_char(d1,'iw')第几周
from x1

 8.确定某月的第一个和最后一个“”周内某天“的日期

select next_day(trunc(sysdate,'mm')-1,2) as 第一个星期一 ,
next_day(last_day(trunc(sysdate))-7,2) as 最后一个星期一
from dual

说明:next_day是“下一”的意思。每个星期第一天是周日,第二天才是周一。

9.创建本月日历

with x0 as
/*取出当前日期月初月末*/
(select trunc(sysdate,'mm') as first_day,last_day(trunc(sysdate,'mm')) as last_day from dual),
x1 as
/*这个月的天数*/
(select (last_day-first_day+1) as day_num ,first_day,last_day from x0 ),
x2 as
/*枚举本月每一天*/
(select first_day+level-1 as dd from x1 connect by level<=day_num),
x3 as
/*提取周信息*/
(select
to_char(dd,'iw') 第几周 ,
to_char(dd,'dd') as 日期,
to_number(to_char(dd,'d')) 周几
from x2
)
select min(case when 周几 ='2' then 日期 end ) as 周一,
max(case when 周几 ='3' then 日期 end) as 周二,
max(case when 周几 ='4' then 日期 end) as 周三,
max(case when 周几 ='5' then 日期 end) as 周四,
max(case when 周几 ='6' then 日期 end) as 周五,
max(case when 周几 ='7' then 日期 end) as 周六,
max(case when 周几 ='1' then 日期 end) as 周日
from x3 group by 第几周 order by 第几周

结果:

 10.创建本年日历

with x0 as
/*取出当前日期月初月末*/
(select trunc(sysdate,'yy') as first_day,add_months(trunc(sysdate,'yy'),12)-1 as last_day from dual),
x1 as
/*这个月的天数*/
(select (last_day-first_day+1) as day_num ,first_day,last_day from x0 ),
x2 as
/*枚举本月每一天*/
(select first_day+level-1 as dd from x1 connect by level<=day_num),
x3 as
/*提取周信息*/
(select
to_char(dd,'iw') 第几周 ,
to_char(dd,'mm') 月,
dd as 日期,
to_number(to_char(dd,'d')) 周几
from x2
),
x4 as
/*修正十二月份53周变为第一周的问题*/
( select case when 月='12' and 第几周='01' then '53' else 第几周 end 第几周,日期, 周几 from x3 )
select min(case when 周几 ='2' then 日期 end ) as 周一,
max(case when 周几 ='3' then 日期 end) as 周二,
max(case when 周几 ='4' then 日期 end) as 周三,
max(case when 周几 ='5' then 日期 end) as 周四,
max(case when 周几 ='6' then 日期 end) as 周五,
max(case when 周几 ='7' then 日期 end) as 周六,
max(case when 周几 ='1' then 日期 end) as 周日
from x4 group by 第几周 order by 第几周

11.确定指定年份季度的开始日期和结束日期

select
sn as 季度,
(sn-1)*3+1 as 月份,
add_months(to_date(年,'yyyy'),(sn-1)*3) as 开始日期,
add_months(to_date(年,'yyyy'),sn*3)-1 as 结束日期
from (select '2013' as 年,level as sn from dual connect by level<=4)

结果:

季度    月份    开始日期    结束日期
1    1    1    2013/3/1    2013/5/31
2    2    4    2013/6/1    2013/8/31
3    3    7    2013/9/1    2013/11/30
4    4    10    2013/12/1    2014/2/28

(013)每日SQL学习:日期的各种计算的更多相关文章

  1. (011)每日SQL学习:SQL开窗函数

    开窗函数:在开窗函数出现之前存在着很多用 SQL 语句很难解决的问题,很多都要通过复杂的相关子查询或者存储过程来完成.为了解决这些问题,在 2003 年 ISO SQL 标准加入了开窗函数,开窗函数的 ...

  2. (009)每日SQL学习:Oracle各个键说明(转)

    原文地址:http://www.agiledata.org/essays/keys.html 本文概述关系数据库中为表指定主键的策略.主要关注于何时使用自然键或者代理键的问题.有些人会告诉你应该总是使 ...

  3. (004)每日SQL学习:物化视图之二

    一.    物化视图概述 Oracle的物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照. 物化视图可 ...

  4. (003)每日SQL学习:普通视图和物化视图

    关于这一点一直就是很懵懂的状态,今天特意网上查了一下资料,以下摘抄网上比较好的答案.以作记录. 普通视图和物化视图的区别答曰:普通视图和物化视图根本就不是一个东西,说区别都是硬拼到一起的,首先明白基本 ...

  5. (014)每日SQL学习:oracle下lag和lead分析函数

    /*语法*/ lag(exp_str,offset,defval) over() Lead(exp_str,offset,defval) over() --exp_str要取的列 --offset取偏 ...

  6. (012)每日SQL学习:TO_CHAR(DATE,FORMAT)

    SYSDATE 2009-6-16 15:25:10 TRUNC(SYSDATE) 2009-6-16 TO_CHAR(SYSDATE,'YYYYMMDD') 20090616 到日 TO_CHAR( ...

  7. (010)每日SQL学习:按字母顺序排列字符串

    需求:一串字母'ADFGH',需要按照顺序来显示:A D F G H 第一步:先把字符串拆分 with test as( select 'ADFGH' as a from dual ) select ...

  8. (008)每日SQL学习:Oracle Not Exists 及 Not In 使用

    今天遇到一个问题,not in 查询失效,我以为是穿越了,仔细查了点资料,原来理解有误! select value from temp_a a where a.id between 1 and 100 ...

  9. (007)每日SQL学习:将字符和数字分离

    with aa as ( select 'sad10' as data from dual union all select 'datf20' as data from dual union all ...

随机推荐

  1. 华为全栈AI技术干货深度解析,解锁企业AI开发“秘籍”

    摘要:针对企业AI开发应用中面临的痛点和难点,为大家带来从实践出发帮助企业构建成熟高效的AI开发流程解决方案. 在数字化转型浪潮席卷全球的今天,AI技术已经成为行业公认的升级重点,正在越来越多的领域为 ...

  2. Sentinel入门学习记录

    最近公司里面在进行微服务开发,因为有使用到限流降级,所以去调研学习了一下Sentinel,在这里做一个记录. Sentinel官方文档:https://github.com/alibaba/Senti ...

  3. arp欺骗(理论)

    ARP(地址解析协议)在IPv4和以太网的广泛应用,其主要用作将IP地址翻译为以太网的MAC地址. 一.ARP通讯协议过程 局域网的通信不是根据IP地址进行,计算机是根据mac地址来识别一台机器. 每 ...

  4. 听说又有兄弟因为用YYYY-MM-dd被锤了...

    还记得去年分享过一篇日期格式化使用 YYYY-MM-dd 的潜在问题的文章不? 历史又重演了... 事故现场 我们来写个单元测试,重现一下这个问题. 测试逻辑: 创建两个日期格式化,一个是出问题的YY ...

  5. 常见数据库的JDBC URL

    转自:http://blog.csdn.net/ring0hx/article/details/6152528 Microsoft SQL Server Microsoft SQL Server JD ...

  6. Linux嵌入式学习-USB端口号绑定

    由于ubuntu USB设备号为从零开始依次累加,所以多个设备每次开机后设备号不固定,机器人每次开机都要蛋疼的按顺序插, 在网上找到一种方法:udev的规则 udev的规则说明,可以参考博客说明:ht ...

  7. java判断路径是文件夹还是文件

    当给定一个路径没有后缀的时候,很难分辨代码是文件还是文件夹,如下图: 我在桌面建立了一个名为one的文件,路径为:/Users/XXXXXX/Desktop/one java代码如下: import ...

  8. Linux 路由 策略路由

    Linux 路由 策略路由 目录 Linux 路由 策略路由 一.路由表 编辑路由表配置文件:/etc/iproute2/rt_tables添加删除修改路由表 二.IP策略 查看IP策略 添加IP策略 ...

  9. RocketMQ(十):数据存储模型设计与实现

    消息中间件,说是一个通信组件也没有错,因为它的本职工作是做消息的传递.然而要做到高效的消息传递,很重要的一点是数据结构,数据结构设计的好坏,一定程度上决定了该消息组件的性能以及能力上限. 1. 消息中 ...

  10. js--数组的every()和some()方法检测数组是否满足条件的使用介绍

    前言 阅读本文之前先来思考一个问题,如何如实现判断一个数组中是否存在满足条件的元素,如何去判断数组中是否全部元素都满足判断条件,这里可能能想到使用for循环遍历数组,if()判断每一项是否符合条件,同 ...