SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date and Time dimension creation and population T-SQL


Date and Time dimension creation and population T-SQL

Author
Message
applebynick
applebynick
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 135
This is a wonderful bit of code. Pity I didn't find it about 4 hrs ago Smile . 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
applebynick
applebynick
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 135
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
Manie Verster
Manie Verster
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1617 Visits: 1022
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.Cool

:-PManie Verster
Developer
Johannesburg
South Africa

I can do all things through Christ who strengthens me. - Holy Bible
I am a man of fixed and unbending principles, the first of which is to be flexible at all times. - Everett Mckinley Dirkson (Well, I am trying. - Manie Verster)
jonathan.despain
jonathan.despain
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 64
These are great!! Thanks for sharing the scripts.
jwalker8680
jwalker8680
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 118
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
------------------------------------------------------------------------
Mark-443607
Mark-443607
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 83
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!!!
Mark-443607
Mark-443607
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 83
Anyone... would be a great help... thank you.
Charles Kincaid
Charles Kincaid
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1623 Visits: 2384
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() ))



ATBCharles Kincaid
Mark-443607
Mark-443607
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 83
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??
Mark-443607
Mark-443607
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 83
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 :-P
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