0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Accessで日時の加算減算

Last updated at Posted at 2022-08-25

やり方

DateAdd()を使う。
〇日前としたければ第二引数を負にすればいい。

DateAdd()でよく使う指定の動作確認
SELECT 
NOW() AS [現在日時],
DateAdd('yyyy', 1, NOW()) AS [現在日時から1年後],
DateAdd('m', 1, NOW()) AS [現在日時から1ヶ月後],
DateAdd('d', 1, NOW()) AS [現在日時から1日後],
DateAdd('h', 1, NOW()) AS [現在日時から1時間後],
DateAdd('n', 1, NOW()) AS [現在日時から1分後],
DateAdd('s', 1, NOW()) AS [現在日時から1秒後]
DateAdd()であまり使わない指定の動作確認
SELECT 
NOW() AS [現在日時],
DateAdd('q', 1, NOW()) AS [現在日時から四半期後],
DateAdd('y', 1, NOW()) AS [現在日時から1年間通算日後],
DateAdd('w', 1, NOW()) AS [現在日時から1週日後],
DateAdd('ww', 1, NOW()) AS [現在日時から1週後]

1月31日の1ヶ月後はどういう結果になる?

SELECT DateAdd('m', 1, '2020/01/31') 

上記の結果は2021/02/29。

うるう日が考慮された2月の月末日が返ってくる。
2021/03/02とはならない。
まして2021/02/31なんていう存在しない日付が返ってくることもない。

うるう日の1年後はどういう結果になる?

SELECT DateAdd('yyyy', 1, '2020/02/29')

上記の結果は2021/02/28。

うるう日が考慮された2月の月末日が返ってくる。
2021/03/01とはならない。

蛇足

『一定期間経ったらファイル削除』なんて時は『〇日後』と要件定義した方が楽。
『〇月後』『〇年後』だと『月ごとに月間日数が違う』や『うるう年』の考慮が面倒。

参考サイトさん

バージョン

Windows 10 Pro 21H2 OSビルド 19044.1889
Microsoft Access for Microsoft 365 MSO (バージョン 2207 ビルド 16.0.15427.20166) 32 ビット

TODO:AccessにはNOW() + INTERVAL 3 DAYのような構文があるのか調べる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?