Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?

DB2 - Counting the number of weeks in a month

Posted on 2017-06-02
3
Medium Priority
?
51 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Join & Write a Comment

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

705 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