Go Premium for a chance to win a PS4. Enter to Win

x
?

DB2 - Counting the number of weeks in a month

Posted on 2017-06-02
3
Medium Priority
?
68 Views
Last Modified: 2017-07-31
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!
0
Comment
Question by:Karen Powers
  • 2
3 Comments
 

Author Comment

by:Karen Powers
ID: 42160670
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 42160706
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
 

Author Comment

by:Karen Powers
ID: 42160805
OK, I got it working. Thank you!!!!
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Join & Write a Comment

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question