

 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/)を参照してください。

# TIMESTAMP\_CMP 関数
<a name="r_TIMESTAMP_CMP"></a>

2 つのタイムスタンプの値を比較し、整数を返します。タイムスタンプが同じである場合、関数は `0` を返します。最初のタイムスタンプがより大きい場合、関数は `1` を返します。2 番目のタイムスタンプがより大きい場合、関数は `-1` を返します。

## 構文
<a name="r_TIMESTAMP_CMP-synopsis"></a>

```
TIMESTAMP_CMP(timestamp1, timestamp2)
```

## 引数
<a name="r_TIMESTAMP_CMP-arguments"></a>

 *timestamp1*   
データ型 `TIMESTAMP` の列または `TIMESTAMP` 型に暗黙的に評価される式。

 *timestamp2*   
データ型 `TIMESTAMP` の列または `TIMESTAMP` 型に暗黙的に評価される式。

## 戻り型
<a name="r_TIMESTAMP_CMP-return-type"></a>

INTEGER

## 例
<a name="r_TIMESTAMP_CMP-examples"></a>

次の例では、タイムスタンプ間を比較し、比較の結果を示します。

```
SELECT TIMESTAMP_CMP('2008-01-24 06:43:29', '2008-01-24 06:43:29'), TIMESTAMP_CMP('2008-01-24 06:43:29', '2008-02-18 02:36:48'), TIMESTAMP_CMP('2008-02-18 02:36:48', '2008-01-24 06:43:29');

timestamp_cmp  | timestamp_cmp | timestamp_cmp 
---------------+---------------+---------------
             0 |            -1 |             1
```

次の例では、出品の LISTTIME と SALETIME を比較します。販売のタイムスタンプは出品のタイムスタンプより後であるため、すべての出品で TIMESTAMP\_CMP の値は `-1` です。

```
select listing.listid, listing.listtime,
sales.saletime, timestamp_cmp(listing.listtime, sales.saletime)
from listing, sales
where listing.listid=sales.listid
order by 1, 2, 3, 4
limit 10;

 listid |      listtime       |      saletime       | timestamp_cmp
--------+---------------------+---------------------+---------------
      1 | 2008-01-24 06:43:29 | 2008-02-18 02:36:48 |            -1
      4 | 2008-05-24 01:18:37 | 2008-06-06 05:00:16 |            -1
      5 | 2008-05-17 02:29:11 | 2008-06-06 08:26:17 |            -1
      5 | 2008-05-17 02:29:11 | 2008-06-09 08:38:52 |            -1
      6 | 2008-08-15 02:08:13 | 2008-08-31 09:17:02 |            -1
     10 | 2008-06-17 09:44:54 | 2008-06-26 12:56:06 |            -1
     10 | 2008-06-17 09:44:54 | 2008-07-10 02:12:36 |            -1
     10 | 2008-06-17 09:44:54 | 2008-07-16 11:59:24 |            -1
     10 | 2008-06-17 09:44:54 | 2008-07-22 02:23:17 |            -1
     12 | 2008-07-25 01:45:49 | 2008-08-04 03:06:36 |            -1
(10 rows)
```

この例は、両方のタイムスタンプが同一の場合に TIMESTAMP\_CMP が 0 を返すことを示しています。

```
select listid, timestamp_cmp(listtime, listtime)
from listing
order by 1 , 2
limit 10;

 listid | timestamp_cmp
--------+---------------
      1 |             0
      2 |             0
      3 |             0
      4 |             0
      5 |             0
      6 |             0
      7 |             0
      8 |             0
      9 |             0
     10 |             0
(10 rows)
```