我一直试图从月份编号中查找季度编号,即我的月份编号 2 应该是季度编号 1,我该如何在 Access VBA 或访问查询中执行此操作? 预先感谢。
您可以使用此功能:
Public Function Quarter(ByVal MonthNumber As Long) As Long
If (MonthNumber < 1) Or (MonthNumber > 12) Then
Call Err.Raise(6) ' Value out of Bounds '
End If
Let Quarter = (MonthNumber - 1) \ 3 + 1
End Function
在 Excel VBA 中,我喜欢使用“选择”,因为有时我需要获取财务季度而不是日历季度。
calendar_quarter = Choose(Month(Date), 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
' if financial year starts in April - this will get the financial quarter from the current date
financial_quarter = Choose(Month(Date), 4, 4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3)
Excel 的一个衬垫:
=INT((MonthNo-1)/3 +1)
对于 SQL
floor(([MonthNo]-1)/3) +1
格式函数会将日期转换为日历季度:
字符串格式: 季度 = 格式((日期), "Q")
或者使用 Val 转换为数字格式: 季度 = Val(格式((日期), "Q"))