日期函數

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   星期三



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章