Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Fun with business days, calendar tables, and test-driven development


Fun with business days, calendar tables, and test-driven development

Author
Message
Alexander Kuznetsov
Alexander Kuznetsov
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 824
Comments posted to this topic are about the item Fun with business days, calendar tables, and test-driven development
RichB
RichB
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1219 Visits: 1024
Hi

Just a suggestion - but when you are doing your workday bit - you should really ensure you get the right setting for datefirst into the script, otherwise you might find it going horribly wrong...



feakesj
feakesj
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 17
The article tries to cover too much ground and ends up doing none of the topics justice. In particular, the first function described would have been much better implemented as an additional column on the calendar table -- a key benefit of such tables is that they allow results to be looked up rather than calculated.
Iulian -207023
Iulian -207023
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1304 Visits: 1230
Very nice article Alex, thank you.

Indeed IsBusinessDate 'Y' or 'N' is great for building an understanding of the concepts, I remember when I implemented this functionality using something similar; I used the name of the holiday in the HolidayName field ('Sunday', 'Saturday', 'Easter', 'Independence day', etc. ) and considering as business day the records where HolidayName is null or empty.
This helped me when I was asked "why there are less working days this month?" :-), and I had the answer at hand. Also it was easy for the HR to define company's holidays as they might change during the year, by just typing the name of the holiday in the interface.

I am curious in approaching multiple calendars, i.e. for different departments or for different geographies.

For example: National days are different for most of the countries but also some religious holidays, Italian branch of one company has the Easter holiday in another period of time than Russian branch.

Also for production department some Saturdays might be business/working days since for sales dept. Saturdays might be weekends/non business days.

Kind regards,
Iulian Cozma
wildh
wildh
Old Hand
Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)

Group: General Forum Members
Points: 318 Visits: 675
I'd agree with feakesj, we've got an 'is working day' bit field. Work out the days by counting that value between the two dates, simple. Looks this is an over engineered solution. However, I will admit that this solution dosnt work well if multiple sites/ business units have different holidays, but then again neither would the articles solution.



Andrew Notarian
Andrew Notarian
SSChasing Mays
SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)

Group: General Forum Members
Points: 657 Visits: 429
Someone should really make a list of US Federal Holidays. You would think that data.gov would have something like that.

As many have done before me, I ended up making my own table. None of the solutions I found online addressed the move of fixed-date holidays to prior Fridays or following Mondays. And most of them had Groundhog Day in there like that actually means anything outside Punxsutawney.
wildh
wildh
Old Hand
Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)

Group: General Forum Members
Points: 318 Visits: 675
Andrew,

Is this any good for you? - http://www.opm.gov/operating_status_schedules/fedhol/2010.asp

btw, Groundhog Day was on TV this weeend in the UK, hadn't seen it in years Smile



Andrew Notarian
Andrew Notarian
SSChasing Mays
SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)SSChasing Mays (657 reputation)

Group: General Forum Members
Points: 657 Visits: 429
wildh (9/23/2010)
Andrew,

Is this any good for you? - http://www.opm.gov/operating_status_schedules/fedhol/2010.asp

btw, Groundhog Day was on TV this weeend in the UK, hadn't seen it in years Smile


This is the master list as provided by the US government, but it's not very easy to parse.
Someone needs to make this list into something clean like XML, JSON, etc. Hell, even a CSV would be better than parsing 10 web pages with weekday names and optional asterisks.
wildh
wildh
Old Hand
Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)Old Hand (318 reputation)

Group: General Forum Members
Points: 318 Visits: 675
Is this any good - http://facility9.com/2009/04/23/populating-us-federal-holidays-in-a-calendar-table/
or -
http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

We have a similar issue in the UK but it's only a 10 min job to crib it from a web site into a update script. But hang on, then it's a 4 month wait for the DBA's to run it.



Robert J
Robert J
SSC Rookie
SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)SSC Rookie (31 reputation)

Group: General Forum Members
Points: 31 Visits: 94
Many times I have had to ask what portion of this date range was non-work time.
This does indeed vary by geography so we have added gmt start and end for each holiday.
We then have to handle four cases to determine the overlaps so the function has to sum the time from UNION ALL of the cases.

I can post this function if anyone is interested but if there is a better way please let me know.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search