[英]How to get last thursday of every month in a year 2013 in oracle?
How to get last Thursday of every month in a year 2013 in oracle? 如何在oracle 2013中获取每月的最后一个星期四? i need to update this date into my table.
我需要将此日期更新到我的表中。
i need a output like 我需要像这样的输出
Last Thursday in a year 2013
----------------------
31.01.2013
28.02.2013
28.03.2013
24.04.2013
30.05.2013
27.06.2013
25.07.2013
29.08.2013
26.09.2013
31.10.2013
28.11.2013
26.12.2013
Thanks to do the needful. 感谢做有需要的人。
This will do it: 这样做:
select next_day (last_day (add_months(date '2013-01-01', rownum-1))-7, 'THU') as thurs
from dual
connect by level <= 12;
THURS
---------
31-JAN-13
28-FEB-13
28-MAR-13
25-APR-13
30-MAY-13
27-JUN-13
25-JUL-13
29-AUG-13
26-SEP-13
31-OCT-13
28-NOV-13
26-DEC-13
12 rows selected.
Explanation: 说明:
1) The following select
is a way to generate a series of integers 1..12: 1)以下
select
是一种生成一系列整数1..12的方法:
select rownum from dual connect by level <= 12;
2) This returns the 1st of each of the 12 months of 2012 by taking 1st January 2013 and adding 0 months, 1 month, ..., 11 months: 2)以2013年1月1日为准,并加上0个月,1个月,...,11个月,从而返回2012年12个月中的每个月的1日:
select add_months(date '2013-01-01', rownum-1)
from dual connect by level <= 12;
3) The last_day
function returns the last day of the month for the given date, so that we now have 2013-01-31, 2013-02-28, ..., 2013-12-31. 3)
last_day
函数返回给定日期的月份的最后一天,因此我们现在有了2013-01-31、2013-02-28,...,2013-12-31。
4) next_day (date, 'THU')
returns the next Thursday after the specified date. 4)
next_day (date, 'THU')
返回指定日期之后的下一个星期四。 To get the last Thursday of the month we take the last day of the month, go back 7 days, then find the next Thursday. 要获得该月的最后一个星期四,我们需要使用该月的最后一天,返回7天,然后找到下一个星期四。
I'd go with dbms_scheduler
: 我会使用
dbms_scheduler
:
declare
start_dt date := date '2013-01-01';
months_last_thursday date;
begin
loop
dbms_scheduler.evaluate_calendar_string (
calendar_string => 'FREQ=MONTHLY;BYDAY=-1 THU',
start_date => start_dt,
return_date_after => start_dt,
next_run_date => months_last_thursday
);
exit when months_last_thursday > date '2013-12-31';
dbms_output.put_line(months_last_thursday);
start_dt := months_last_thursday;
end loop;
end;
/
Building on @Rene's answer, this could be extended into a general (generic?) solution: 以@Rene的答案为基础,可以将其扩展为一般(通用)解决方案:
CREATE OR REPLACE FUNCTION recurring_dates (p_year IN NUMBER, p_rule IN VARCHAR)
RETURN dbms_stats.datearray PIPELINED
AS
start_dt DATE;
last_dt DATE;
next_dt DATE;
BEGIN
start_dt := to_date(to_char(p_year,'fm0000')||'01-01','YYYY-MM-DD');
last_dt := to_date(to_char(p_year,'fm0000')||'12-31','YYYY-MM-DD');
LOOP
dbms_scheduler.evaluate_calendar_string(
calendar_string => p_rule,
start_date => start_dt,
return_date_after => start_dt,
next_run_date => next_dt);
EXIT WHEN next_dt > last_dt;
PIPE ROW (next_dt);
start_dt := next_dt;
END LOOP;
END recurring_dates;
/
You'd feed the function with a calendar string in the DBMS_SCHEDULER
-Syntax, and it will return the matching dates. 您将在
DBMS_SCHEDULER
-Syntax中为该函数提供日历字符串,它将返回匹配的日期。
@rcmuthu786's last thursdays: @ rcmuthu786的最后一个星期四:
SELECT * FROM TABLE(recurring_dates (2013, 'FREQ=MONTHLY;BYDAY=-1 THU'));
2013-01-31
2013-02-28
2013-03-28
2013-04-25
2013-05-30
...
Or, the second Wednesdays of each month: 或者,每个月的第二个星期三:
SELECT * FROM TABLE(recurring_dates (2013, 'FREQ=MONTHLY;BYDAY=2 WED'));
2013-01-09
2013-02-13
2013-03-13
2013-04-10
...
etc, etc... 等,等等。
The most elegant way is the following 最优雅的方法如下
select thdate from ( select a.*, row_number() over (partition by to_char(thdate,'yyyymm'),day order by thdate desc) row_rank from ( select trunc(sysdate,'YEAR') + rownum-1 thdate, trim(to_char(trunc(sysdate,'YEAR') + rownum-1,'DAY')) day from dba_objects where trunc(sysdate,'YEAR') + rownum-1 < trunc(sysdate+365,'YEAR') ) a where day='THURSDAY' ) where row_rank=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.