Adding Years, Months and Days

  • Comments posted to this topic are about the item Adding Years, Months and Days

  • Hey Pete -- this is top work.  Much appreciated, Nobel Prize in the post.  Steve Rothkopf, Thinking Software

  • Only one thing missing -- the Scottish Bank Holidays...

  • You can get the UK bank holidays from the API on gov.uk i.e. https://www.gov.uk/api/bank-holidays.json

  • funbi - Tuesday, March 14, 2017 6:15 AM

    You can get the UK bank holidays from the API on gov.uk i.e. https://www.gov.uk/api/bank-holidays.json

    Thanks, will have a look -- I was hoping to find that Pete could incorporate the Scottish Bank Holidays into this script -- you listening, Pete?

  • Nice work, Peter! Thanks for posting this up.
    Couple of suggestions...you might be able to cut the reads down on the ros table using something like this:
    ;WITH ros AS (
     SELECT VolunteerID,
      DateFrom = Cast(ROS.DateFrom As Date),
      DateTo =  Cast(IsNull(ROS.DateTo , GetDate()) As Date)
     FROM dbo.RecordOfService
    )
    SELECT
     t.VolunteerID,
        [FromWhen] = CONVERT(nvarchar(12), t.FromWhen, 105),
        [ToWhen] = CONVERT(nvarchar(12), t.ToWhen, 105),
        t.NoOfWorkingDays,
        [VirtualEndDate] = DATEADD(D, t.NoOfWorkingDays - 1, t.FromWhen),
        [DateDiffAsYMD] = dbo.fnDateDiffsYMD(t.FromWhen, DATEADD(D, t.NoOfWorkingDays - 1, t.FromWhen))
    FROM (
     SELECT
      ros.VolunteerID,
      [NoOfWorkingDays] = COUNT(DISTINCT d.[Date]),
      [FromWhen] = MIN(ros.DateFrom),
      [ToWhen] = MAX(ros.DateTo)
     FROM dbo.DimDate d
     INNER JOIN ros
      ON d.[Date] BETWEEN ros.DateFrom AND ros.DateTo
     GROUP BY ros.VolunteerID
    ) t

    Secondly, that UDF will inhibit parallelism - I wonder if it can be converted to an inline table-valued function...

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks for the various responses, which are much appreciated.  I have fixed thousands of problems over the years, but the vast majority are not that exciting - problems resolved by asking 'Is your Caps Lock on?' tend not to lend themselves to interesting articles.  Although in the interests of accuracy, it would only be fair to state that my wife did not find this adding of dates problem all that interesting!  Who knows, I may get a few more interesting problems to write about in due course.

    I did have a look at a 'simplified' view of Scottish Public Holidays, and was shocked to see how complex it is.  I can't think how it works out in practice, where folk in each town may have different holidays from the town down the road.  I would think that somebody who lives in that environment would be far better placed to write the required tweaks to the Public Holiday Calculation.  The specific Irish requirements by contrast look quite straightforward - I would add a 'Scope' field, which would describe which area would get the holiday (which could be pretty complex in Scotland!).

    And thanks to ChrisM for his contributions.  I can say that my query looked really good compared to some of the clunky queries that they used, but there is always room for improvement.  My experience to date with parallelism has been somewhat mixed, although I don't doubt that it will at some point be the grail that we all pursue.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply