在Oracle是提供了next_day求指定日期的下一个日期.
语法 : next_day( date, weekday )
date is used to find the next weekday.
weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY)
可用于:
Oracle 9i, Oracle 10g, Oracle 11g
举例:
可以求出当前日期的下个星期一。
select next_day( sysdate, 'MONDAY') from dual;
使用方法并不复杂,主要注意如下:
1) 当第二个参数传的星期数比现有星期数小的时候,会返回下一个星期的日期;当第二个参数所传的星期数比 现有的星期数大的时候,则会返回本周的相应星期日期。
例如:
ps:请注意如下红色字体。
SQL> select sysdate from dual;
SYSDATE
-------------------
2010-12-30 19:30:26
1 row selected.
Elapsed: 00:00:00.01
SQL> select next_day(sysdate,'tuesday') from dual;
NEXT_DAY(SYSDATE,'T
-------------------
2011-01-04 18:57:37
1 row selected.
Elapsed: 00:00:00.01
SQL>
SQL> select next_day(sysdate,'MONDAY') from dual;
NEXT_DAY(SYSDATE,'M
-------------------
2011-01-03 18:58:45
1 row selected.
Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'WEDNESDAY') from dual;
NEXT_DAY(SYSDATE,'W
-------------------
2011-01-05 18:59:15
1 row selected.
Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'THURSDAY') from dual;
NEXT_DAY(SYSDATE,'T
-------------------
2011-01-06 18:59:30
1 row selected.
Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'FRIDAY') from dual;
NEXT_DAY(SYSDATE,'F
-------------------
2010-12-31 18:59:43
1 row selected.
Elapsed: 00:00:00.00
SQL> select next_day(sysdate,'SATURDAY') from dual;
NEXT_DAY(SYSDATE,'S
-------------------
2011-01-01 19:00:19
1 row selected.
Elapsed: 00:00:00.01
SQL> select next_day(sysdate,'SUNDAY') from dual;
NEXT_DAY(SYSDATE,'S
-------------------
2011-01-02 19:00:37
1 row selected.
Elapsed: 00:00:00.00
2)好的方法
统计上个星期日到上个星期六的总和,
具体写法:
l_begin_date ~ l_end_date:
NEXT_DAY(SYSDATE-7*2-1,'sunday') ~ NEXT_DAY(SYSDATE-7-2,'saturday')
然后再在程序中作判断:
IF l_begin_date>NEXT_DAY(SYSDATE-7*2-1,'sunday')
--限制统计开始日期必须是周日
OR TO_CHAR(l_begin_date,'d')<>1
--限制统计结束日期必须是周六
OR TO_CHAR(l_end_date,'d')<>7
--限制开始日期和结束日期时间间隔必须是一周
OR l_end_date-l_begin_date<>6
THEN
raise_application_error(-20503,'you enter error parameter');
END IF;
--每周一跑数据,其它时候空跑
IF TO_CHAR(SYSDATE,'d')=2
THEN
*****************
end IF;
原地址:http://blog.csdn.net/ningjieshuijing/article/details/6107965