

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# DATE\$1DIFF 函數
<a name="DATE_DIFF_function"></a>

DATE\$1DIFF 傳回兩個日期或時間表達式的日期部分之間的差異。

## 語法
<a name="DATE_DIFF_function-synopsis"></a>

```
date_diff(endDate, startDate)
```

## 引數
<a name="DATE_DIFF_function-arguments"></a>

 *endDate*   
DATE 表達式。

*startDate*  
DATE 表達式。

## 傳回類型
<a name="DATE_DIFF_function-return-type"></a>

BIGINT

## 具有 DATE 欄的範例
<a name="DATE_DIFF_function-examples"></a>

下列範例會尋找在兩個常值日期值間週次的差異。

```
select date_diff(week,'2009-01-01','2009-12-31') as numweeks;

numweeks
----------
52
(1 row)
```

下列範例會尋找兩個常值日期值之間的差異 (以小時為單位)。如果您未提供日期的時間值，則預設值為 00:00:00。

```
select date_diff(hour, '2023-01-01', '2023-01-03 05:04:03');
            
date_diff
----------
53
(1 row)
```

下列範例會尋找兩個常值 TIMESTAMETZ 值之間的差異 (以天為單位)。

```
Select date_diff(days, 'Jun 1,2008  09:59:59 EST', 'Jul 4,2008  09:59:59 EST')
         
date_diff
----------
33
```

以下範例找出資料表中同一列兩個日期之間的差異 (以天為單位)。

```
select * from date_table;

start_date |   end_date
-----------+-----------
2009-01-01 | 2009-03-23
2023-01-04 | 2024-05-04
(2 rows)

select date_diff(day, start_date, end_date) as duration from date_table;
         
duration
---------
      81
     486
(2 rows)
```

下列範例會尋找在過去和今日日期中常值間季次的差異。此範例假設目前日期為 2008 年 6 月 5 日。您可以用全名或縮寫來表示日期部分。DATE\$1DIFF 函數的預設欄名稱為 DATE\$1DIFF。

```
select date_diff(qtr, '1998-07-01', current_date);

date_diff
-----------
40
(1 row)
```

下列範例會聯結 SALES 和 LISTING 資歷表，來計算在他們列出和門票售出後所經的天數：清單 1000 到 1005。這些清單銷售的最長等待時間是 15 天，而最短時間是不到一天 (0 天)。

```
select priceperticket,
date_diff(day, listtime, saletime) as wait
from sales, listing where sales.listid = listing.listid
and sales.listid between 1000 and 1005
order by wait desc, priceperticket desc;

priceperticket | wait
---------------+------
 96.00         |   15
 123.00        |   11
 131.00        |    9
 123.00        |    6
 129.00        |    4
 96.00         |    4
 96.00         |    0
(7 rows)
```

此範例會計算賣家等待所有門票特價的平均小時數。

```
select avg(date_diff(hours, listtime, saletime)) as avgwait
from sales, listing
where sales.listid = listing.listid;

avgwait
---------
465
(1 row)
```

## 具有 TIME 欄的範例
<a name="DATE_DIFF_function-examples-time"></a>

下列範例資料表 TIME\$1TEST 有一個 TIME\$1VAL 欄 (類型為 TIME)，其中插入了三個值。

```
select time_val from time_test;
            
time_val
---------------------
20:00:00
00:00:00.5550
00:58:00
```

下列範例會尋找 TIME\$1VAL 欄與時間常值之間的小時數差異。

```
select date_diff(hour, time_val, time '15:24:45') from time_test;
         
 date_diff
-----------
        -5
        15
        15
```

下列範例會尋找兩個常值時間值之間的分鐘數差異。

```
select date_diff(minute, time '20:00:00', time '21:00:00') as nummins;  
         
nummins 
---------- 
60
```

## 具有 TIMTZ 欄的範例
<a name="DATE_DIFF_function-examples-timetz"></a>

下列範例資料表 TIMETZ\$1TEST 有一個 TIMETZ\$1VAL 欄 (類型為 TIMETZ)，其中插入了三個值。

```
select timetz_val from timetz_test;
            
timetz_val
------------------
04:00:00+00
00:00:00.5550+00
05:58:00+00
```

下列範例會尋找 TIMETZ 常值與 timetz\$1val 之間的小時數差異。

```
select date_diff(hours, timetz '20:00:00 PST', timetz_val) as numhours from timetz_test;

numhours 
---------- 
0
-4
1
```

下列範例會尋找兩個常值 TIMTZ 值之間的小時數差異。

```
select date_diff(hours, timetz '20:00:00 PST', timetz '00:58:00 EST') as numhours;
         
numhours 
---------- 
1
```