将周数和年份转换为 Access-SQL 中的日期?

Convert week number and year to a date in Access-SQL?

在 Microsoft Access 中,我有一个 table,其中每个工作日的工作小时数与项目编号、ISO 周编号和年份相关联。简化后,它看起来像这样:

ProjectID WeekNumber YearNumber Monday Tuesday
1 1 2022 5 6
1 2 2022 7 8

我正在尝试设置一个查询,其中所有工作日列(周一到周日)都“合并”到一个列中,并将周和年数字转换为日期。它应该看起来像这样:

ProjectID Date HoursPerDay
1 03.01.2022 5
1 04.01.2022 6
1 10.01.2022 7
1 11.01.2022 8

我设法使用 UNION 查询将所有工作日列合并为一列:

SELECT ProjectID, WeekNumber, YearNumber, Monday As HoursPerDay FROM ProjectHours
UNION ALL
SELECT ProjectID, WeekNumber, YearNumber, Tuesday As HoursPerDay FROM ProjectHours;

结果如下所示:

ProjectID WeekNumber YearNumber HoursPerDay
1 1 2022 5
1 1 2022 6
1 2 2022 7
1 2 2022 8

但我无法将等周和年份数字转换为日期。这在 Access-SQL?

中完全可行吗

我发现 on Whosebug and went through the date/time functions Microsoft 列出了 Access 但无法使其正常工作。

非常感谢任何帮助。

编辑:社区成员提出了这个问题的可能重复项,但我正在寻找可以在 Access-SQL 查询中使用的解决方案,所以建议的副本对我没有帮助,因为它建议基于 VBA 的解决方案。

这不是那么简单,因为 ISO 年份很少与日历年同步。

但是这个函数可以:

' Returns the date of Monday for the ISO 8601 week of IsoYear and Week.
' Optionally, returns the date of any other weekday of that week.
'
' 2017-05-03. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateYearWeek( _
    ByVal IsoWeek As Integer, _
    Optional ByVal IsoYear As Integer, _
    Optional ByVal DayOfWeek As VbDayOfWeek = VbDayOfWeek.vbMonday) _
    As Date
    
    Dim WeekDate    As Date
    Dim ResultDate  As Date
    
    If IsoYear = 0 Then
        IsoYear = Year(Date)
    End If
    
    ' Validate parameters.
    If Not IsWeekday(DayOfWeek) Then
        ' Don't accept invalid values for DayOfWeek.
        Err.Raise DtError.dtInvalidProcedureCallOrArgument
        Exit Function
    End If
    If Not IsWeek(IsoWeek, IsoYear) Then
        ' A valid week number must be passed.
        Err.Raise DtError.dtInvalidProcedureCallOrArgument
        Exit Function
    End If
    
    WeekDate = DateAdd(IntervalSetting(dtWeek), IsoWeek - 1, DateFirstWeekYear(IsoYear))
    ResultDate = DateThisWeekPrimo(WeekDate, DayOfWeek)
    
    DateYearWeek = ResultDate

End Function

但是,它使用了一系列支持函数,例如:

' Returns the primo date of the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateThisWeekPrimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = 0
    Interval = IntervalSetting(DtInterval.dtWeek)
    
    ResultDate = DateIntervalPrimo(Interval, Number, DateThisWeek, FirstDayOfWeek)
    
    DateThisWeekPrimo = ResultDate
    
End Function

还有更多 - 此处 post 太多了。

因此,请参阅我在 GitHubVBA.Date 的项目,以获取包含完整代码的模块。