|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, August 29, 2012 2:55 AM
Points: 87,
Visits: 109
|
|
This is a wonderful bit of code. Pity I didn't find it about 4 hrs ago :) . Just 1 thing. If I want the WeekOfMonth to start on a Monday, Where and how do I need to alter the code. I can see it is somewhere in lines 142 , 143 " , DATEPART(ww,@Date) + 0 - -----changed to set Monday as Day 1 DATEPART(ww,CAST(DATEPART(mm,@Date) AS VARCHAR) + '/1/' + CAST(DATEPART(yy,@Date) AS VARCHAR)) [WeekOfMonth]"
and as you can see I have tried with amending the + but no luck.
What I am trying to achieve is having the week of Month set to 1 for the first full week of the Mmonth starting from the fisrt Monday.
Once again, excellent code and please advise.
Nick
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Wednesday, August 29, 2012 2:55 AM
Points: 87,
Visits: 109
|
|
Excellent code. Wish I had found this several hours back. Can you advise how to amend this so week of month starts on Moday as opposed to Sunday. Many thanks
Nick
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 5:33 AM
Points: 1,162,
Visits: 896
|
|
Dunno really what to make of this script. I'll have to take time and go through it some more but I like the idea of setting up tables with holidays but unfortunately every country's holidays is different so a lot of editing will be required.
Manie Verster Developer Johannesburg South Africa
Life is about choices.... I choose to be happy today
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, August 03, 2012 9:25 AM
Points: 5,
Visits: 63
|
|
| These are great!! Thanks for sharing the scripts.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 31, 2013 6:57 AM
Points: 30,
Visits: 111
|
|
The code for figuring out the week of the month is built in SQL functionality... I don't know that there is a way, non-manual, to calculate week of month if the week starts on Monday instead of Sunday... I'm sure you can do it by putting in a loop and checking day one... and counting the days from there... etc... but that would take a lot of code and a lot of time to loop through every day for 150 years...
Yeah... it was a bit of work getting it to this point and I wish someone had done it like this before me to save me all that time... but I figured I'd save everyone else a bit of time... 
Your Welcome... Your Welcome... Your Welcome...
PS>... For the holiday's comment... yeah... Those are mostly manual as you can see in the code... pick and choose your own... and if anyone has a way of programatically calculating when the Jewish calender holidays are...etc... I'd love to see it and add it to this code for everyone.
As for easter... I found the code posted by AUTHOR: Robert Davis SOURCE: http://www.databasejournal.com/scripts/article.php/3469911/Calculate-Easter-for-Any-Year.htm
Add this code to the script I posted after you add the function from the link above. --Easter ------------------------------------------------------------------------------------------------------\ DECLARE @Years TABLE([ID] INT IDENTITY(1,1), [YEAR][INT])
INSERT INTO @YEARS([YEAR]) SELECT DISTINCT YEAR FROM [dim_DATE]
DECLARE @POS BIGINT, @CNTR BIGINT, @YEAR VARCHAR(4)
SELECT @POS = 1, @CNTR = MAX([ID]) FROM @YEARS
WHILE @POS <= @CNTR BEGIN SELECT @YEAR = [YEAR] FROM @YEARS WHERE ID = @POS
UPDATE dim_DATE SET HolidayText = ISNULL(HolidayText,'') + 'Easter' WHERE DATE = CAST(dbo.Year2Easter(@YEAR) AS DATETIME) SELECT @POS = @POS + 1 END GO ------------------------------------------------------------------------
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, October 09, 2012 4:20 AM
Points: 14,
Visits: 80
|
|
Wonderful code... could someone assist with a function that uses dim_Date table(s) and works something like http://www.sqlservercentral.com/Forums/Topic207135-23-1.aspx but also accommodates for holidays???
Thinking something like this: weekday_dateadd(dateTime,<# weekdays>,<0/1>Skip holidays,<0/1>DayOnly -drop time)
Select weekday_dateadd(getdate(),10,1,1)
Thank you in advance!!!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, October 09, 2012 4:20 AM
Points: 14,
Visits: 80
|
|
| Anyone... would be a great help... thank you.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, June 11, 2013 11:56 AM
Points: 772,
Visits: 1,828
|
|
Here is how to drop the time from a date/time. I use GetDate() in the example but you can use a column just as well.
SELECT DATEADD(DAY, 0, DATEDIFF(DAY, 0, GetDate() ))
ATB
Charles Kincaid
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, October 09, 2012 4:20 AM
Points: 14,
Visits: 80
|
|
thank you but unfortunately I don't think that will accommodate my need. I need to be able to add days (weekdays only excluding holidays) to a date, my basic need would always be getdate() but more flexible as a function parameter so I can do "BASICALY" getdate()+10 (no weekend day or holiday).
This dim_Date table has everything to do it... but not sure the most efficient way to do it.
Me doing it...I would just give a start date and get X rows in the future that are weekday and not holiday and take max(date) returned... It would work... but most efficient??
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Tuesday, October 09, 2012 4:20 AM
Points: 14,
Visits: 80
|
|
Guess will just post my version maybe will be of some help to someone else.
CREATE FUNCTION [dbo].[weekday_dateadd] ( @startDate SMALLDATETIME, @Days INT, @SkipHoliday BIT = 1, @DayOnly BIT = 1 ) RETURNS SMALLDATETIME AS BEGIN /* Title: weekday_dateadd(2) Author: Mark Wolff Date: 11/18/2010 Purpose: Adds a specified number of weekdays to a given date allowing to SkipHolidays also
Examples: SELECT dbo.weekday_dateadd(GETDATE(),90,DEFAULT,DEFAULT) SELECT dbo.weekday_dateadd(GETDATE(),10,DEFAULT,0)
NOTE: Will not work past a few years */ DECLARE @endDate SMALLDATETIME
SELECT @endDate = date FROM ( SELECT ROW_NUMBER() OVER (ORDER BY date) Sequence ,CASE WHEN @DayOnly = 1 THEN Date ELSE Date + CONVERT(VARCHAR,@startDate,108) END Date FROM dim_Date(NOLOCK) WHERE DAYOFWEEK NOT IN ('Saturday','Sunday') AND (CASE WHEN @SkipHoliday = 1 AND HolidayText IS NOT NULL THEN 0 ELSE 1 END) = 1 AND Date BETWEEN @startDate-1 AND DATEADD(d,@Days+@Days/7*3+20,@startDate) )d WHERE Sequence = @Days
RETURN @endDate END
Did some basic testing seems to work
|
|
|
|