|
What is the quickest/ most effective way in SQL Server of getting the number of working days in a Month (defining the number of working days as being the days monday-friday)
(comments are locked)
|
|
All months always has exactly 20 workdays for the first 28 days. You only have to check day 29, 30 and 31 for being workdays. This code is regional and language independant. This seems to give the right answer most of the time, but when I count up the number of working days in February 2008, your code thinks it is 23 working days and my windows calendar says it is 21 days. Am I doing something wrong.
Oct 13 '09 at 04:04 PM
Andrew Clarke
Change "UNION ALL" to "UNION".
Oct 13 '09 at 08:02 PM
Peso
Did the change from "UNION ALL" to "UNION" solve your problem?
Nov 06 '09 at 08:17 AM
Peso
(comments are locked)
|
|
Here's a way:
You can read this post on my blog to see why I am using @@DateFirst. Obviously this does not take holidays into account. You'd need a calendar table for that.
(comments are locked)
|
|
Another way is to use a numbers table and a little date math.
(comments are locked)
|
|
SELECT to_char(Mdate, 'dy'), To_char(Mdate, 'DD-Mon-YYYY') FROM (select To_date (Level || '/' || '&month' || '/' || &year,'DD-Mon-YYYY') Mdate from dual connect by level <= last_day(to_date('01-' || '&month' || '-' || &year,'DD-Mon-YYYY')) - to_date('01-' || '&month' || '-' || &year, 'DD-Mon-YYYY') + 1) WHERE to_char(Mdate, 'dy') <> 'sun' AND to_char(Mdate, 'dy') <> 'sat'; In this Query The Month input should be in mon Format like : jan,Feb,Mar,Apr And Year like YYYY : 2012,2011 it may ask for input 3 times depends on SQL operator ALL D BEST.......
(comments are locked)
|
|
I know this is an old thread but what I thought was an obvious solution did not rate a mention. You'll be coming back to this kind of thing time and time again, so why not do all the calculations in advance and store the results in a permanent calendar table (a table of dates - one date per row). Need local public holidays? Add a column that flags them (and even a column that names them if you like). Need phases of the moon? Again, throw in another column. A flag for weekends? First and last day of the month? Week starting date? Financial quarter? Anything that relates to dates will benefit from this approach and make your code much easier to read.
(comments are locked)
|


Do you want to allow for bank holidays? And if so, how localized do you want to be?!
Just to make sure you see it. My post below does not take holidays into account, but is localized.
I'm not worried about public holidays.