|
Re: want to get number of sundays in a month [message #197353 is a reply to message #197346] |
Wed, 11 October 2006 01:00 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Feels like homework time again.
Still, it helps to get my brain warmed up in a morning, so here's two completely different solutions.
One works out the number of sundays based on the number of days in the month and the day the month starts on, and the other uses a row generator to step through the month one day at a time, counting the sundays.
Note that both of these are NLS specific. Some NLS regions have Sunday as day 1 of the week, not day 7.
select floor((last_day(trunc(dte)) - trunc(dte,'MM'))/7) -- 1 per complete week
,case when to_char(trunc(dte,'MM'),'D') + mod(last_day(trunc(dte)) - trunc(dte,'MM'),7) >= 7 then 1
else 0
end
from (select to_date('07-07-2006','dd-mm-yyyy') dte from dual);
select sum(decode(to_char(dte,'D'),7,1,0))
from (select trunc(to_date('07-10-2006','dd-mm-yyyy'),'MM')+level -1 dte
from dual
connect by level <= to_char(last_day(to_date('07-10-2006','dd-mm-yyyy')),'dd'));
|
|
|
Re: want to get number of sundays in a month [message #197355 is a reply to message #197346] |
Wed, 11 October 2006 01:05 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
If you search the board you should be able to find a solution but it has been quite a while since it was posted so, here's one:
SQL> select count(thedate) x
2 from ( select to_date('200605','YYYYMM')+level-1 thedate
3 from dual
4 connect by level < add_months(to_date('200605','YYYYMM'),1)
5 - to_date('200605','YYYYMM')
6 )
7 where to_char(thedate,'DY','NLS_DATE_LANGUAGE=AMERICAN') = 'SUN'
8 /
X
----------
4 This shows the number of Sundays in May 2006. The third parameter in the to_char ('NLS_DATE_LANGUAGE') is to make sure Oracle takes the American (English?) abbreviation of the day of the week ("SUN") and is optional (and rarely used).
MHE
[Updated on: Wed, 11 October 2006 01:06] Report message to a moderator
|
|
|
|
|
|