日期函数

1、用trunc( )函数截取日期作为月初
select hiredate 雇用日期, trunc(hiredate, 'mm') 月初 from emp where rownum <= 1;
雇用日期    月初
----------- -----------
1980/12/17  1980/12/1

例子:
select hiredate,
       to_number(to_char(hiredate, 'hh24')) 时,
       to_number(to_char(hiredate, 'mi')) 分,
       to_number(to_char(hiredate, 'ss')) 秒,
       to_number(to_char(hiredate, 'dd')) 日,
       to_number(to_char(hiredate, 'mm')) 月,
       to_number(to_char(hiredate, 'yyyy')) 年,
       to_number(to_char(hiredate, 'ddd')) 年内第几天,
       trunc(hiredate, 'dd') 一天之始,
       trunc(hiredate, 'day') 周初,
       trunc(hiredate, 'mm') 月初,
       last_day(hiredate) 月末,
       add_months(trunc(hiredate, 'mm'), 1) 下月初,
       trunc(hiredate, 'yy') 年初,
       to_char(hiredate, 'day') 周几,
       to_char(hiredate, 'month') 月份
  from (select hiredate + 30 / 24 / 60 / 60 + 20 / 24 / 60 + 5 / 24 as hiredate
          from emp
         where rownum <= 1);

HIREDATE             时          分          秒          日       月       年    年内第几天 一天之始    周初                                                                                                                          ---------- -------- ---------- ---------- ----------- ----------- ----------- -----------  ----------- -----------       

1980/12/17           5         20         30         17      12     1980   352  1980/12/17  1980/12/14  
月初                   月末             下月初                  年初      周几       月份   
  --------------- ----------            ----------           ---------- -------- ----------
1980/12/1   1980/12/31  1981/1/1    1980/1/1    星 期三      12月  
2、interval 存放的是时间间隔的信息,
 select interval '2' year as "year",
    interval '25' month as "month",
    interval '88' day as "day",
    interval '80' hour as "hour",
    interval '90' minute as"minute",
    interval '3.18' second as "second" 
from dual;
   
year              month         day               hour         minute              second
----------  --------------  ---------------    ------------   ------------    --------------------
+02-00           +02-01        +88 00:00:00     +03 08:00:00  +00 01:30:00     +00 00:00:03.180000
                                           
3、extract 和to_char功能是一样的,extract可以提取时间字段里的年月日时分秒,extract返回值的类型是number类型的。
4、确定哪一年是闰年
SQL> select last_day( add_months(trunc(sysdate,'y'),1)) from dual;
LAST_DAY(ADD_MONTHS(TRUNC(SYSD
------------------------------
2016/2/29
/*截取时间年初*/ 
SQL> select trunc(sysdate,'y') from dual;
TRUNC(SYSDATE,'Y')
------------------
2016/1/1
截取月初
SQL> select trunc(sysdate,'mm') from dual;
TRUNC(SYSDATE,'MM')
-------------------
2016/9/1
5、确定一年里属于周内某一天的所有日期
SQL> select add_months(trunc(sysdate,'y'),12)-trunc(sysdate,'y') from dual;
ADD_MONTHS(TRUNC(SYSDATE,'Y'),
------------------------------
                           366
SQL> select add_months(trunc(sysdate,'y'),12),trunc(sysdate,'y') from dual;
ADD_MONTHS(TRUNC(SYSDATE,'Y'), TRUNC(SYSDATE,'Y')
------------------------------ ------------------
2017/1/1                       2016/1/1
select trunc(sysdate, 'y') + (level - 1)
  from dual
connect by level <=
           (add_months(trunc(sysdate, 'y'), 12)) - trunc(sysdate, 'y');

TRUNC(SYSDATE,'Y')+(LEVEL-1)
----------------------------
2016/1/1
2016/1/2
2016/1/3
2016/1/4
2016/1/5
2016/1/6
2016/1/7
2016/1/8
2016/1/9
2016/1/10
2016/1/11
2016/1/12
2016/1/13
2016/1/14
...

查询一年里,属于星期五的所有日期
with t as (select trunc(sysdate, 'y') + (level - 1) dy
  from dual
connect by level <=
           (add_months(trunc(sysdate, 'y'), 12)) - trunc(sysdate, 'y'))
select dy,to_char(dy,'day') as 周五 from t where to_char(dy,'d')=6;
DY          周五
----------- ---------
2016/1/1    星期五
2016/1/8    星期五
2016/1/15   星期五
2016/1/22   星期五
2016/1/29   星期五
2016/2/5    星期五
2016/2/12   星期五
2016/2/19   星期五
2016/2/26   星期五
2016/3/4    星期五
2016/3/11   星期五
2016/3/18   星期五
2016/3/25   星期五
2016/4/1    星期五
2016/4/8    星期五
2016/4/15   星期五
2016/4/22   星期五
2016/4/29   星期五
2016/5/6    星期五
2016/5/13   星期五
DY          周五
----------- ---------
2016/5/20   星期五
2016/5/27   星期五
2016/6/3    星期五
2016/6/10   星期五
2016/6/17   星期五
2016/6/24   星期五
2016/7/1    星期五
2016/7/8    星期五
2016/7/15   星期五
2016/7/22   星期五
2016/7/29   星期五
2016/8/5    星期五
2016/8/12   星期五
2016/8/19   星期五
2016/8/26   星期五
2016/9/2    星期五
2016/9/9    星期五
2016/9/16   星期五
2016/9/23   星期五
2016/9/30   星期五
2016/10/7   星期五
DY          周五
----------- ---------
2016/10/14  星期五
2016/10/21  星期五
2016/10/28  星期五
2016/11/4   星期五
2016/11/11  星期五
2016/11/18  星期五
2016/11/25  星期五
2016/12/2   星期五
2016/12/9   星期五
2016/12/16  星期五
2016/12/23  星期五
2016/12/30  星期五
53 rows selected
6、确定一个月内第一个和最后一个“周内某天”的日期
确定九月第一个星期三,和最后一个星期三
select next_day(trunc(sysdate, 'mm') - 1, 4) d,
       to_char(next_day(trunc(sysdate, 'mm') - 1, 4), 'day') day
  from dual;
D           DAY
----------- ---------
2016/9/7    星期三

select next_day(last_day(trunc(sysdate, 'mm')) - 7, 4) 下周三,
       to_char(next_day(last_day(trunc(sysdate, 'mm')) - 7, 4), 'day') day
  from dual;
下周三      DAY
----------- ---------
2016/9/28   星期三



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值