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


Adding Years, Months and Days


Adding Years, Months and Days

Author
Message
RevOX11
RevOX11
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 36
Comments posted to this topic are about the item Adding Years, Months and Days
steve.rothkopf
steve.rothkopf
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 10
Hey Pete -- this is top work. Much appreciated, Nobel Prize in the post. Steve Rothkopf, Thinking Software
steve.rothkopf
steve.rothkopf
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 10
Only one thing missing -- the Scottish Bank Holidays...
funbi
funbi
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 557
You can get the UK bank holidays from the API on gov.uk i.e. https://www.gov.uk/api/bank-holidays.json
steve.rothkopf
steve.rothkopf
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 10
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?

ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13844 Visits: 19436
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
Exploring Recursive CTEs by Example Dwain Camps
RevOX11
RevOX11
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 36
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.
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