

 Amazon Redshift は、パッチ 198 以降、新しい Python UDF の作成をサポートしなくなります。既存の Python UDF は、2026 年 6 月 30 日まで引き続き機能します。詳細については、[ブログ記事](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)を参照してください。

# 日付関数またはタイムスタンプ関数の日付部分
<a name="r_Dateparts_for_datetime_functions"></a>

次のテーブルは、次の関数に対する引数として受け取る、日付部分および時刻部分の名前と略名を指定します。
+ DATEADD 
+ DATEDIFF 
+ DATE\$1PART 
+ EXTRACT 

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/redshift/latest/dg/r_Dateparts_for_datetime_functions.html)

## 結果のバリエーション (秒、ミリ秒、マイクロ秒）
<a name="r_Dateparts_for_datetime_functions-variations-in-results"></a>

異なる日付関数が秒、ミリ秒、またはマイクロ秒を日付部分として指定する場合、クエリ結果にわずかな違いが生じます。
+ EXTRACT 関数は、上位および下位の日付部分は無視し、指定された日付部分のみの整数を返します。指定された日付部分が秒の場合、ミリ秒およびマイクロ秒は結果に含まれません。指定された日付部分がミリ秒の場合、秒およびマイクロ秒は結果に含まれません。指定された日付部分がマイクロ秒の場合、秒およびミリ秒は結果に含まれません。
+ DATE\$1PART 関数は、指定された日付部分にかかわらず、タイムスタンプの完全な秒部分を返します。必要に応じて小数値または整数を返します。

例えば、次のクエリの結果を比較します。

```
create table seconds(micro timestamp);

insert into seconds values('2009-09-21 11:10:03.189717');

select extract(sec from micro) from seconds;
               
date_part
-----------
3
               
select date_part(sec, micro) from seconds;
   
pgdate_part
-------------
3.189717
```

## CENTURY、EPOCH、DECADE、および MIL ノート
<a name="r_Dateparts_for_datetime_functions-century"></a>

CENTURY または CENTURIES   
Amazon Redshift は CENTURY を *\$1\$1\$11* の年に始まり `###0` の年に終了すると解釈します。  

```
select extract (century from timestamp '2000-12-16 12:21:13');
date_part
-----------
20

select extract (century from timestamp '2001-12-16 12:21:13');
date_part
-----------
21
```

EPOCH   
Amazon Redshift の EPOCH の実装は、クラスターのあるタイムゾーンから独立した 1970-01-01 00:00:00.000000 に関連します。クラスターが設置されているタイムゾーンによって、時差による結果を補正する必要がある場合があります。  
 次の内容の例を以下に示します。  

1.  EVENT テーブルに基づいて EVENT\$1EXAMPLE というテーブルを作成します。この CREATE AS コマンドは、DATE\$1PART 関数を使用してデータ列 (デフォルトでは PGDATE\$1PART と名づけられる) を作成し、各イベントのエポック値を保存します。

1.  PG\$1TABLE\$1DEF から EVENT\$1EXAMPLE の列とデータタイプを選択します。

1.  EVENT\$1EXAMPLE テーブルから EVENTNAME、STARTTIME と PGDATE\$1PART を選択して、日付と時間の複数の形式を表示します。

1.  EVENTNAME と STARTTIME をそれぞれ選択します。1 秒間隔のタイムゾーンを指定しないタイムスタンプを使って PGDATE\$1PART のエポック値を変換し、CONVERTED\$1TIMESTAMP という列にその結果を返します。

```
create table event_example
as select eventname, starttime, date_part(epoch, starttime) from event;

select "column", type from pg_table_def where tablename='event_example';

     column    |            type
---------------+-----------------------------
 eventname     | character varying(200)
 starttime     | timestamp without time zone
 pgdate_part   | double precision
(3 rows)
```

```
select eventname, starttime, pgdate_part from event_example;

   eventname          |      starttime      | pgdate_part
----------------------+---------------------+-------------
 Mamma Mia!           | 2008-01-01 20:00:00 |  1199217600
 Spring Awakening     | 2008-01-01 15:00:00 |  1199199600
 Nas                  | 2008-01-01 14:30:00 |  1199197800
 Hannah Montana       | 2008-01-01 19:30:00 |  1199215800
 K.D. Lang            | 2008-01-01 15:00:00 |  1199199600
 Spamalot             | 2008-01-02 20:00:00 |  1199304000
 Macbeth              | 2008-01-02 15:00:00 |  1199286000
 The Cherry Orchard   | 2008-01-02 14:30:00 |  1199284200
 Macbeth              | 2008-01-02 19:30:00 |  1199302200
 Demi Lovato          | 2008-01-02 19:30:00 |  1199302200

   
select eventname, 
starttime, 
timestamp with time zone 'epoch' + pgdate_part * interval '1 second' AS converted_timestamp 
from event_example;

       eventname      |      starttime      | converted_timestamp
----------------------+---------------------+---------------------
 Mamma Mia!           | 2008-01-01 20:00:00 | 2008-01-01 20:00:00
 Spring Awakening     | 2008-01-01 15:00:00 | 2008-01-01 15:00:00
 Nas                  | 2008-01-01 14:30:00 | 2008-01-01 14:30:00
 Hannah Montana       | 2008-01-01 19:30:00 | 2008-01-01 19:30:00
 K.D. Lang            | 2008-01-01 15:00:00 | 2008-01-01 15:00:00
 Spamalot             | 2008-01-02 20:00:00 | 2008-01-02 20:00:00
 Macbeth              | 2008-01-02 15:00:00 | 2008-01-02 15:00:00
 The Cherry Orchard   | 2008-01-02 14:30:00 | 2008-01-02 14:30:00
 Macbeth              | 2008-01-02 19:30:00 | 2008-01-02 19:30:00
 Demi Lovato          | 2008-01-02 19:30:00 | 2008-01-02 19:30:00
 ...
```

DECADE または DECADES   
Amazon Redshift は共通カレンダーに基づいて DECADE または DECADES DATEPART を解釈します。例えば、共通カレンダーが年 1 から始まるため、最初の 10 年 (decade 1) は 0001-01-01 から 0009-12-31 であり、2 番目の 10 年 (decade 2) は 0010-01-01 から 0019-12-31 です。例えば、decade 201 は 2000-01-01 から 2009-12-31 の期間に及びます。  

```
select extract(decade from timestamp '1999-02-16 20:38:40');
date_part
-----------
200

select extract(decade from timestamp '2000-02-16 20:38:40');
date_part
-----------
201

select extract(decade from timestamp '2010-02-16 20:38:40');
date_part
-----------
202
```

MIL または MILS   
Amazon Redshift は MIL を *\$1001* の年の初めの日に始まり `#000` の年の最後の日に終了すると解釈します。  

```
select extract (mil from timestamp '2000-12-16 12:21:13');
date_part
-----------
2

select extract (mil from timestamp '2001-12-16 12:21:13');
date_part
-----------
3
```