Results 1 to 7 of 7
  1. #1
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53

    convert date to calender week

    he guys I have a query with date, but I need the calender week. Do you have any advises for me how I could convert from date to calender week inside a query?

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    You need to write a public function that accepts a date as an argument and returns the calendar week. Place the function in a standard module. When using the function in SQL do not qualify it with the module name.

    For example suppose the function is called CalcCalWeek and your date field is called MyDate then the design grid specification is:

    CalendarWeek: CalcCalWeek([MyDate])

    or the in-line SQL

    CalcCalWeek([MyDate]) As CalendarWeek

  3. #3
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    Its really urgent,

    do you have any code examples for me

    thanks in advance

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    No because each and every organisation divides the calendar in different ways. Week1 for you may not be week 1 for me.

  5. #5
    snoopy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Posts
    53
    I created my own. First I extracted the access query to Excel and then I collected the following vba-code

    Public Function IsoWeekNumber(d1 As Date) As Integer
    Dim d2 As Long
    If IsNull(d1) Then
    IsoWeekNumber = 0
    End If
    d2 = DateSerial(Year(d1 - Weekday(d1 - 1) + 4), 1, 3)
    IsoWeekNumber = Int(d1 - d2 + Weekday(d2) + 5) / 7
    End Function


    So, actually there should be no internal company guideline for converting calender weeks, because there is a standard by ISO. There are also some standards by national organisations. Why to reinvent the wheel, anyway
    That is one main reason for using standards.

    After it you just use this function in your excel sheet. Attention you need two worksheets, one with date as coming from database query and one, which converts into calender week.

    here is the code for the field:

    =IF(Table1[@[ROW]],IsoweekNumber(Table1[@[ROW]]) & "CW / " & YEAR(Table1[@[ROW]]),"")


    That's it

  6. #6
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Snoopy thanks for pointing out the ISO standard.

    ISO 8601 was introduced in 1988, and subsequently modified in 2000 and 2004. The year 1988 was after the last time I worked with week numbers so no wonder I was unaware of the standard. I'm still puzzled as to whether the Muslim nations follow this standard. Anyone know?

    Glad to see you have it working in Excel. Didn't you originally want an Access function?

    PS - Later

    It's all there:

    DatePart("ww", dte, vbMonday, vbFirstFourDays)
    Last edited by Rod; 10-29-2012 at 02:44 AM. Reason: Added PS

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Just to add a reference (C Pearson's site http://www.cpearson.com/excel/WeekNumbers.aspx)

    ISO Week Number

    This method uses the International Standards Organization (ISO) definition of a week number. An ISO week always begins on a Monday, and ISO week 1 begins on the Monday of the first week of the year that contains a Thursday. Said differently, ISO week 1 is the first week (beginning on Monday) that has at least four days.
    Note that it is possible that the first few days of a year may fall into week 52 or week 53. Although this may seem quite counter intuitive, it follows from the requirements first that all ISO weeks begin on Monday, and second, that the first week must contain a Thursday. For example, the year 2010 begins on Friday, 1-Jan-2010. Since this is later than a Thursday (which is 7-Jan-2010), the week doesn't contains Thursday (or, said another way, the week doesn't have at least four days), the ISO week 1 begins on Monday, 4-January-2010. The day before the start of the year, 31-December-2009, falls in ISO week 53 of the preceeding year, 2009. Rather than deal with the ambiguity of having a day that is in both week 53 and week 0, the 53 carries through until 4-Jan-2010, which as noted before is the Monday of the first week that contains a Thursday (or, equivalently, the first week with four or more days).
    The VBA code for calculating an ISO week number is shown below:

    Public Function IsoWeekNumber(InDate As Date) As Long
    IsoWeekNumber = DatePart("ww", InDate, vbMonday, vbFirstFourDays)
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Adding the day of the week to a date field
    By cschieff in forum Reports
    Replies: 2
    Last Post: 10-24-2012, 02:47 PM
  2. Replies: 3
    Last Post: 04-01-2012, 01:40 PM
  3. Group by Week for any given start date
    By royalrochelle in forum Queries
    Replies: 3
    Last Post: 02-15-2012, 08:44 PM
  4. how to convert week number to date range
    By qwang1115 in forum Access
    Replies: 1
    Last Post: 02-13-2009, 11:35 AM
  5. Retrieving Week of Year in a date range
    By Korvega in forum Queries
    Replies: 2
    Last Post: 05-27-2006, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums