Link to home
Start Free TrialLog in
Avatar of DBADS
DBADSFlag for Egypt

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
Avatar of Christopher Gordon
Christopher Gordon
Flag of United States of America image

SELECT DATEPART(week, @thedate)
Avatar of _DJ
_DJ

SELECT DATEPART(WEEK, GETDATE())
OR
SELECT DATEPART(wk, GETDATE())
OR
SELECT DATEPART(ww, GETDATE())
Avatar of DBADS

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
What about something like this...

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
Avatar of DBADS

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...
Avatar of DBADS

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.
SOLUTION
Avatar of Christopher Gordon
Christopher Gordon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial