に最終更新
MINUTES_DIFF
説明
MINUTES_DIFF 関数は、二つの日時の差を分単位で計算します。終了時刻から開始時刻を引いた結果を分単位で返します。DATE、DATETIME、TIMESTAMP_NS をサポートします。
構文
MINUTES_DIFF(`<date_or_time_expr1>`, `<date_or_time_expr2>`)
パラメータ
| パラメータ | 説明 |
|---|---|
<date_or_time_expr1> | 終了時刻。型は DATE、DATETIME、または TIMESTAMP_NS。 |
<date_or_time_expr2> | 開始時刻。型は DATE、DATETIME、または TIMESTAMP_NS。 |
戻り値
<date_or_time_expr1> と <date_or_time_expr2> の差を分単位で表す INT 型の整数を返します(1 時間 = 60 分)。
<date_or_time_expr1>が<date_or_time_expr2>より後の場合、正の数を返します。<date_or_time_expr1>が<date_or_time_expr2>より前の場合、負の数を返します。- 実際の時間差に基づいて計算し、秒やマイクロ秒を無視しません。
- 入力が DATE 型(年、月、日のみ)の場合、時刻部分はデフォルトで 00:00:00 になります。
- 入力日時にゼロ以外の小数秒、秒、またはマイクロ秒の部分がある場合、それらも計算に含まれます。
- いずれかのパラメータが NULL の場合、NULL を返します。
使用例
-- Minute difference when end time is greater than start time
SELECT MINUTES_DIFF('2020-12-25 22:00:00', '2020-12-25 21:00:00') AS result;
+--------+
| result |
+--------+
| 60 |
+--------+
-- Includes scale, calculation does not ignore it
SELECT MINUTES_DIFF('2020-12-25 21:00:00.999', '2020-12-25 22:00:00.923');
+--------------------------------------------------------------------+
| MINUTES_DIFF('2020-12-25 21:00:00.999', '2020-12-25 22:00:00.923') |
+--------------------------------------------------------------------+
| -59 |
+--------------------------------------------------------------------+
-- End time is earlier than start time, returns negative number
SELECT MINUTES_DIFF('2023-07-13 21:50:00', '2023-07-13 22:00:00') AS result;
+--------+
| result |
+--------+
| -10 |
+--------+
-- Input is of DATE type (default time 00:00:00)
SELECT MINUTES_DIFF('2023-07-14', '2023-07-13') AS result;
+--------+
| result |
+--------+
| 1440 |
+--------+
-- Two times have different seconds, seconds are also calculated
SELECT MINUTES_DIFF('2023-07-13 22:30:59', '2023-07-13 22:31:01') AS result;
+--------+
| result |
+--------+
| 0 |
+--------+
-- Any parameter is NULL, returns NULL
SELECT MINUTES_DIFF(NULL, '2023-07-13 22:00:00'), MINUTES_DIFF('2023-07-13 22:00:00', NULL) AS result;
+-------------------------------------------+--------+
| MINUTES_DIFF(NULL, '2023-07-13 22:00:00') | result |
+-------------------------------------------+--------+
| NULL | NULL |
+-------------------------------------------+--------+