1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLで日付範囲指定 (SQL Server)

Last updated at Posted at 2022-02-18

SQLで日付範囲指定

BETWEEN を使って、下記のようにする。

SELECT * FROM table1 WHERE date_col BETWEEN '2022-02-17 00:00:00' AND '2022-03-17 00:00:00';

日付の文字列の形式(スタイル)が違う場合

例えば、日付のスタイルが、'2022-02-17 00:00:00' ではなく、'20220217' の場合(yyyymmddの形式)に対応したいとき。

__CONVERT__を使う。

SELECT * FROM table1 WHERE CONVERT(VARCHAR(10),date_col,112) BETWEEN '20220217' AND '20220317';

CONVERTの3つ目の引数112は スタイルで、スタイルには、112 = yyyymmdd , 111 = yyyy/mm/dd など、様々なものがあるようです。

参考

1
3
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
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?