2

I need to make my Access query always return the Monday of the current week. I have seen a few solutions on Google/StackOverflow but they are written in SQL and I am a beginner in creating Access queries (I am using the Design view to make them).

目標: 週はMTWTFS Sと見なす必要があります。次に、クエリは常に現在の週の月曜日を返す必要があります。したがって、日曜日の場合でも、次の週の月曜日ではなく、前の月曜日を返す必要があります。Access 2010 のデザイン ビューを使用してこれを行う方法を説明できる人はいますか?

4

2 に答える 2

9

このコンテキストでは日付を扱っていることに注意してください。したがって、そうすると、Date() - 1今日より 1 日早くなります。

Date()〜今日の日付

DatePart(
        "w" - Weekday
        Date() - Today's date
        2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
        1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
)

-1 - Subtract 1 from the DatePart value.

Date() = 4/27/2015 (at time of this writing)
DatePart("w",Date(),2,1) = 1
DatePart("w",Date(),2,1)-1 = 0

だから私たちはDate()-0... わかりました、それの何がそんなに素晴らしいのですか? さて、今日の日付が月曜日以外の日である、より有用なシナリオを見てみましょう。

今日が2015/4/28(火)だとしよう

Date() = 4/28/2015
DatePart("w",Date(),2,1) = 2
DatePart("w",Date(),2,1)-1 = 1

だから、外側から、内側に。現在の平日の値を教えてください。weekday(1 = 月曜日、2 = 火曜日など)、それから 1 を引く ->値 1 (月曜日)に戻すには、現在の日付から何日引く必要があるかを示します。

于 2015-04-27T16:51:02.363 に答える