1
0

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 5 years have passed since last update.

Oracle上にあるテーブルをISO 8601形式の日付で範囲指定して絞込検索を行う方法

Posted at

日付を利用した絞込において、ISO 8601型式の日付を検索に利用すると、タイムゾーンにかかわらず等しい検索結果を得ることができます。

条件

  • テーブル上の日付はtimestamp型でセットされている(=DBのタイムゾーンに応じた日付がセットされている)こと
  • 検索時に指定する日付はtimestamp_tz型でセットされること

コード

select * from <テーブル名>
where 
    sys_extract_utc(<日付列>) >= 
    to_timestamp(
        sys_extract_utc(
            to_timezone_tz(
                '<開始日時(timestamp_tz型で指定)>',
                'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
            )
        )
    )
    and
    sys_extract_utc(<日付列>) >= 
    to_timestamp(
        sys_extract_utc(
            to_timezone_tz(
                '<終了日時(timestamp_tz型で指定)>',
                'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
            )
        )
    )
order by <日付列> asc;

select
    id, /* pk */
    date /* timestamp型 */
from hogetbl
where
    sys_extract_utc(date) >= 
    to_timestamp(
        sys_extract_utc(
            to_timestamp_tz(
                '2019-02-01T00:00:00.000+0000',  
                'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
            )
        )
    )
    and
    sys_extract_utc(date) >= 
    to_timestamp(
        sys_extract_utc(
            to_timestamp_tz(
                '2019-02-01T23:59:59.000+0000',  
                'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
            )
        )
    )
order by date asc;
1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?