Oracle日期查询:季度、月份、星期等时间信息

时间:2024-03-04 17:23:18

--oracle有关时间信息

Select to_char(sysdate,\'Q\') from dual;--指定日期的季度
Select to_char(sysdate,\'MM\') from dual;--月份
Select to_char(sysdate,\'WW\') from dual;--当年第几周
Select to_char(sysdate,\'W\') from dual ;--本月第几周
Select to_char(sysdate,\'DD\') from dual;--当月第几天
Select to_char(sysdate,\'D\') from dual;--周内第几天
Select to_char(sysdate,\'DY\') from duaL;--星期几
Select last_day(sysdate) from dual;--本月最后一天
Select add_months(sysdate,2) from dual;--当前日期d后推n个月 
select months_between(sysdate,to_date(\'2012-11-12\',\'yyyy-mm-dd\'))from dual;--日期f和s间相差月数
SELECT (next_day(sysdate,1)+1) FROM dual;--指定的日期之后的第一个工作日的日期
select to_char(add_months(last_day(sysdate),-1),\'yyyy-MM-dd\') LastDay from dual;--上月末天
select to_char(add_months(sysdate,-1),\'yyyy-MM-dd\') PreToday from dual;--上月今天
select to_char(add_months(last_day(sysdate)+1,-2),\'yyyy-MM-dd\') firstDay from dual;--上月第一天
select to_char(sysdate,\'ww\') from dual group by to_char(sysdate,\'ww\');--按照每周进行统计
select to_char(sysdate,\'mm\') from dual group by to_char(sysdate,\'mm\');--按照每月进行统计
select to_char(sysdate,\'q\') from dual group by to_char(sysdate,\'q\');--按照每季度进行统计

--找出当前月份的周五的日期
select to_char(t.d, \'YY-MM-DD\')
  from (select trunc(sysdate, \'MM\') + rownum - 1 as d
          from dba_objects
         where rownum < 32) t
 where to_char(t.d, \'MM\') = to_char(sysdate, \'MM\')
   and trim(to_char(t.d, \'Day\')) = \'星期五\'
1  select to_char(sysdate,\'iw\')   from dual; --本周是第几个自然周  
2   select to_char(sysdate,\'yyyy\') into v_sbzq_nf from dual;  -- 当前年份  
3   SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,\'yyyy-MM-dd\'),\'yyyy-MM-dd\'),\'IW\'),\'yyyy-MM-dd\')  FROM DUAL;--本周的起始时间(本周周一日期)  
4   SELECT to_char(TRUNC(TO_DATE(to_char(sysdate,\'yyyy-MM-dd\'),\'YYYY-MM-DD\'),\'IW\') + 6,\'yyyy-MM-dd\') FROM DUAL;--本周的结束时间(本周周日日期) 

 

转载于http://blog.csdn.net/dreamthen/article/details/8447306