LoginSignup
6
16

More than 3 years have passed since last update.

OracleのSQLにおいてwhereで日付型のカラムと比較をする

Posted at
  • 環境 : Oracle Database 12c Standard Edition Release 12.2.0.1.0 - 64bit Production

任意の日付と比較する

-- 任意の日時以降を取得する
select * from テーブル名 where 日付型のカラム >= to_date('2019-09-03 22:10:00', 'yyyy-mm-dd hh24:mi:ss');
-- 1日分を取得する
select * from テーブル名 where 日付型のカラム BETWEEN to_date('2019-09-03', 'yyyy-mm-dd') AND to_date('2019-09-04', 'yyyy-mm-dd');

システム日付と比較する

-- システム日付と比較する時はDBの時間がずれていないか確認してから使う
select sysdate from dual;
-- 直近24時間分を取得する
select * from テーブル名 where 日付型のカラム > (sysdate - 1);
-- 今日分を取得する(=昨日の23:59:59より後)
select * from テーブル名 where 日付型のカラム > trunc(sysdate - 1);
select * from テーブル名 where 日付型のカラム > trunc(sysdate - INTERVAL '1' DAY);
-- 直近1時間分を取得する
select * from テーブル名 where 日付型のカラム > (sysdate - 1/24);
select * from テーブル名 where 日付型のカラム > (sysdate - INTERVAL '1' HOUR);
6
16
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
6
16