oracle 日期 查询

0.获得某个月所有天的列表:

select * from 

(select to_char(to_date(to_char(to_date('200802','YYYYMM'), 'yyyymm') || '01', 'yyyymmdd') + rownum - 1,'yyyy-mm-dd') days
          from dual
         start with dummy = 'X'
        connect by dummy = dummy)
 where rownum < to_number(to_char(last_day(to_date('200802','YYYYMM')), 'dd')) + 1;

 

1.判断某一天是周几

select to_char(sysdate,'day') from dual;

select to_char(to_date('2007-11-20','yyyy-mm-dd'),'day') from dual;

2.求某月的天数

select to_char(last_day(sysdate),'dd') days from dual;

select to_char(last_day(to_date('200802','YYYYMM')),'dd') from dual;

3.求某年的天数

select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual;

select add_months(trunc(to_date('2008','YYYY'),'year'), 12) - trunc(to_date('2008','YYYY'),'year') from dual;

4.求下个星期一的日期

在获取之前可以设置日期语言,如:
ALTER SESSION SET NLS_DATE_LANGUAGE='SIMPLIFIED CHINESE';
select next_day(sysdate,'星期一') from dual;
select next_day(sysdate,2) from dual; --后面的数字是从星期日开始算起,所以为2

ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
select next_day(sysdate,'monday') from dual;

5.确定某天是当年的第几周

select to_char(sysdate,'fmww') from dual;

select to_char(to_date('20071126','YYYYMMDD'),'fmww') from dual;


6.确定某天是当月的第几周

select to_char(sysdate,'WW') - to_char(trunc(sysdate,'MM'),'WW') + 1 from dual

select to_char(to_date('20071125','YYYYMMDD'),'WW') - to_char(trunc(to_date('20071125','YYYYMMDD'),'MM'),'WW') + 1 from dual

7.确定某天是当年的第几天

select to_char(sysdate,'DDD') from dual

8.确定某天是当月的第几天

select to_char(sysdate,'DD') from dual;

9.确定某天是一周的第几天

select to_char(sysdate,'D') - 1 from dual; --Oracle定义周日为一周的第一天,所以要减一

10.求两个日期间的天数

select floor(sysdate - to_date('20071125','YYYYMMDD')) from dual;

11.求两个日期间的月数

select floor(months_between(sysdate, to_date('20071031','YYYYMMDD'))) from dual;

12.计算某天的小时、分、秒

select day,
TRUNC(a*24) Hours,
TRUNC(a*24*60 - 60*TRUNC(a*24)) Minutes,
TRUNC(a*24*60*60 - 60*TRUNC(a*24*60)) Seconds
from ( select trunc(sysdate) day, sysdate - trunc(sysdate) a from dual)

你可能感兴趣的:(oracle 日期 查询)