【问题标题】:weeknumbers ms access query周数 ms 访问查询
【发布时间】:2021-04-12 06:43:01
【问题描述】:

我想根据以下条件获取 ms 访问中的周数:

https://www.kalender-365.nl/kalender-2021.html

我的表情:

test: DatePart("ww",#01/01/2021#,2,2)

这将返回 1 而不是周 53。我该怎么做才能返回53

语法:

DatePart(datepart, date, firstdayofweek, firstweekofyear)

2 = 星期一(第一天)

2 = 使用一年中至少有 4 天的第一周 (firstdayofyear)

【问题讨论】:

  • AFAIK Access 不支持真正的 ISO 周数 - 您必须自己进行计算。
  • 啊哈谢谢。这澄清了事情。
  • DatePart("ww",#01/01/2021#,2,2) 正确返回 53,而不是 1。
  • 在设计视图中创建查询时,我的访问 (2007) 奇怪地返回 1 和 DatePart("ww",#01/01/2021#,2,2)(例如,在此处使用此代码时,相同的代码确实返回 53:w3schools.com/sql/…)。你的功能(stackoverflow.com/a/65597984/2968136),但是完美!非常感谢!

标签: ms-access week-number datepart


【解决方案1】:

您可能还需要匹配的 ISO 年份:

Public Const MaxWeekValue           As Integer = 53
Public Const MinWeekValue           As Integer = 1
Public Const MaxMonthValue          As Integer = 12
Public Const MinMonthValue          As Integer = 1



' Returns the ISO 8601 week of a date.
' The related ISO year is returned by ref.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Week( _
    ByVal Date1 As Date, _
    Optional ByRef IsoYear As Integer) _
    As Integer

    Dim Month       As Integer
    Dim Interval    As String
    Dim Result      As Integer
    
    Interval = "ww"
    
    Month = VBA.Month(Date1)
    ' Initially, set the ISO year to the calendar year.
    IsoYear = VBA.Year(Date1)
    
    Result = DatePart(Interval, Date1, vbMonday, vbFirstFourDays)
    If Result = MaxWeekValue Then
        If DatePart(Interval, DateAdd(Interval, 1, Date1), vbMonday, vbFirstFourDays) = MinWeekValue Then
            ' OK. The next week is the first week of the following year.
        Else
            ' This is really the first week of the next ISO year.
            ' Correct for DatePart bug.
            Result = MinWeekValue
        End If
    End If
        
    ' Adjust year where week number belongs to next or previous year.
    If Month = MinMonthValue Then
        If Result >= MaxWeekValue - 1 Then
            ' This is an early date of January belonging to the last week of the previous ISO year.
            IsoYear = IsoYear - 1
        End If
    ElseIf Month = MaxMonthValue Then
        If Result = MinWeekValue Then
            ' This is a late date of December belonging to the first week of the next ISO year.
            IsoYear = IsoYear + 1
        End If
    End If
    
    ' IsoYear is returned by reference.
    Week = Result
        
End Function

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多