DBADS
asked on
How to populate the Week Number inside the Date Dimension?
Hello,
Does anyone have a script or a guide to load the week number (1-53) column in a date dimension
The week starts on Sunday and ends on Saturday.
Thank you
Does anyone have a script or a guide to load the week number (1-53) column in a date dimension
The week starts on Sunday and ends on Saturday.
Thank you
SELECT DATEPART(week, @thedate)
SELECT DATEPART(WEEK, GETDATE())
OR
SELECT DATEPART(wk, GETDATE())
OR
SELECT DATEPART(ww, GETDATE())
OR
SELECT DATEPART(wk, GETDATE())
OR
SELECT DATEPART(ww, GETDATE())
ASKER
Thanks guys for the reply
The problem is the results of the above queries is different than this http://2010-calendar.org/
The query is producing W+1 compared to the calendar in the link
Example: For day 2010-01-01:
The query is producing Week 1, while the link denotes Week 53 of the previous year
Please advise. This is urgent
The problem is the results of the above queries is different than this http://2010-calendar.org/
The query is producing W+1 compared to the calendar in the link
Example: For day 2010-01-01:
The query is producing Week 1, while the link denotes Week 53 of the previous year
Please advise. This is urgent
What about something like this...
SELECT case DATEPART(WEEK, GETDATE())
when 1 then 53
else DATEPART(WEEK, GETDATE()) + 1
end
SELECT case DATEPART(WEEK, GETDATE())
when 1 then 53
else DATEPART(WEEK, GETDATE()) + 1
end
Actually you'd want this...
SELECT case DATEPART(WEEK, GETDATE())
when 1 then 53
else DATEPART(WEEK, GETDATE()) - 1
end
SELECT case DATEPART(WEEK, GETDATE())
when 1 then 53
else DATEPART(WEEK, GETDATE()) - 1
end
ASKER
Unfortunately, it didn't work
Applying your suggested query worked unless the year starts with a Sunday (example 2012)
In such case the first week of the year will be Week 53 instead of 1
I think the query to populate the dimension has to have the calendar week logic in order to cover all cases...
Applying your suggested query worked unless the year starts with a Sunday (example 2012)
In such case the first week of the year will be Week 53 instead of 1
I think the query to populate the dimension has to have the calendar week logic in order to cover all cases...
ASKER
Is there a way to query this logic:
"ISO 8601 defines the Week as always starting with Monday. The first week is the week which contains the first Thursday of the calendar year. This implies that it is the week which is mostly within the Calendar year and the week containing January 4th"
The only difference is that the starting day should be Sunday; instead of Monday.
"ISO 8601 defines the Week as always starting with Monday. The first week is the week which contains the first Thursday of the calendar year. This implies that it is the week which is mostly within the Calendar year and the week containing January 4th"
The only difference is that the starting day should be Sunday; instead of Monday.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.