Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count of Mondays in a Month

Status
Not open for further replies.

thermidor

Programmer
Nov 28, 2001
123
US
Hi All,

Can anyone suggest a query that will tell me how many times a given weekday occurs within a month. I want to know that in Jul 07 there are 5 Mondays; in Aug there are 4 Mondays; etc.

TIA,
Sven
 
Thermidor,

Although you could resolve your need entirely in SQL, it seems to me that a user-defined function would be tigher on an on-going basis:
Code:
create or replace function Count_DoW (date_in date, Day_of_Week varchar2) return number is	
begin
        if date_in is null then return null;
        end if;
        If to_char(next_day(trunc(date_in,'MM')-1
                  ,substr(Day_of_week,1,3))+21,'DD')
           <= to_char(last_day(date_in),'DD')-7 then
            return 5;
        else
            return 4;
        end if;
end;
/

Function created.
Here is its invocation:
Code:
clear col
col a heading "Number|of Mondays|in July"
col b heading "Number|of Mondays|in August"
select count_dow(sysdate,'MON') a, count_dow(sysdate+30,'MON') b
  from dual;

    Number     Number
of Mondays of Mondays
   in July  in August
---------- ----------
         5          4
Let us know if this satisfies your need.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top