DB2 - Counting the number of weeks in a month

I had this question after viewing Number of Weeks in a particular month.

This works great for Saturday but I my week starts on Monday (day 1).  How do I change this so that I can pick a different day, example, Monday (first day) or Sunday (last day) of the week.
Thank you!
Karen PowersAsked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Karen,

The original query took subtle advantage of 1/1/2012 being a Sunday and having the DayOfWeek value of 1.  

From the Saturday() sub-query,

  select startdate + (7 - dayofweek (startdate)) days from daterange

Translates to

  select startdate + (7 - 1) days from daterange.

Which picks the first Saturday.  From there, the query just adds 7 days to the previous result and assembles a list of all Saturdays.

To Select all Mondays, we need to modify that part of the query to grab the first Monday.  The rest of the query should work just fine.

The DayOfWeek function returns

Day   - Su M Tu W Th F Sa
Value - 1  2  3 4  5 6  7

Open in new window

To increment any day to the Monday (or stay on the current data if it is Monday) we need to add to the DayOfWeek value.

Day   - Su M Tu W Th F Sa
Value - 1  2  3 4  5 6  7
Add   - 1  0  6 5  4 3  2

Open in new window


   select startdate + (7 - dayofweek (startdate)) days from daterange

becomes

  select startdate + (9 - dayofweek (startdate)) days from daterange

However, that can miss the first Monday of the month.  So, subtracting 7 from 9 we get:

  select startdate + (2 - dayofweek (startdate)) days from daterange

The complete query:

with
daterange (startdate, enddate)
as
(
  select date('01/01/2012'), date('12/31/2012') from sysibm.sysdummy1
),
Mondays (thisdate)
as
(
  select startdate + (2 - dayofweek (startdate)) days from daterange
  union all
  select thisdate + 7 days from Mondays
  where thisdate + 7 days <= (select enddate from daterange) 
)
SELECT * FROM Mondays;

Open in new window


Note that we're going to want to make things a bit more complicated if the initial date isn't pre-determined to be Sunday.  :)  We should make it bullet-proof so any start date is valid.  But that begs the question, do you want all Mondays on or after that date or Mondays starting with the first Monday of the given month?

Kent
2
 
Karen PowersAuthor Commented:
I am actually doing it for a particular month so I have:
with
daterange (startdate, enddate)
as
(
  select date('05/01/2017'), date('05/30/2017') from sysibm.sysdummy1
),
Mondays (thisdate)
as
(
  select startdate + (7 - dayofweek (startdate)) days from daterange
  union all
  select thisdate + 7 days from Mondays
  where thisdate + 7 days <= (select enddate from daterange)
)
select month (thisdate), count(*) from Mondays
group by month (thisdate)
;

If I change the 7 to another number, it is totally wrong
0
 
Karen PowersAuthor Commented:
OK, I got it working. Thank you!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.