メインコンテンツまでスキップ
に最終更新

MONTH_CEIL

説明​

MONTH_CEIL 関数は、入力した日時を指定した月間隔の境界まで切り上げます。origin を指定した場合はその時刻を起点とし、指定しない場合は 0001-01-01 00:00:00 を起点とします。

日付の計算式:

month_ceil(⟨date_or_time_expr⟩,⟨period⟩,⟨origin⟩)=min⁡{⟨origin⟩+k×⟨period⟩×month∣k∈Z∧⟨origin⟩+k×⟨period⟩×month≥⟨date_or_time_expr⟩}\begin{aligned} &\text{month\_ceil}(\langle\text{date\_or\_time\_expr}\rangle, \langle\text{period}\rangle, \langle\text{origin}\rangle) = \\ &\min\{\langle\text{origin}\rangle + k \times \langle\text{period}\rangle \times \text{month} \mid \\ &k \in \mathbb{Z} \land \langle\text{origin}\rangle + k \times \langle\text{period}\rangle \times \text{month} \geq \langle\text{date\_or\_time\_expr}\rangle\} \end{aligned}

kk は、起点から対象の日時に達するまでに必要な期間の数を表します。

構文​

MONTH_CEIL(`<date_or_time_expr>`)
MONTH_CEIL(`<date_or_time_expr>`, `<origin>`)
MONTH_CEIL(`<date_or_time_expr>`, `<period>`)
MONTH_CEIL(`<date_or_time_expr>`, `<period>`, `<origin>`)

パラメータ​

パラメータ説明
<date_or_time_expr>切り上げる日時の値。DATE、DATETIME、TIMESTAMP_NS、TIMESTAMPTZ 型をサポートする有効な日付式です。DATE 型は、該当する日付の開始時刻 00:00:00 に変換されます。具体的な形式については、timestamptz の変換、datetime の変換、date の変換を参照してください。
<period>月間隔の値。INT 型で、各期間に含まれる月数を表します。
<origin>期間の起点となる日時。DATE、DATETIME、TIMESTAMP_NS 型をサポートします。デフォルト値は 0001-01-01 00:00:00 です。

戻り値​

TIMESTAMP_NS 型の入力に対しては、小数秒が固定の九桁精度の TIMESTAMP_NS 型を返します。結果は戻り値の型の範囲に照らして検証されます。TIMESTAMP_NS 型の範囲は [1677-09-21 00:12:43.145224192, 2262-04-11 23:47:16.854775807] です。

<origin> を省略した TIMESTAMP_NS 型の入力では、文書に記載されたデフォルトの起点は内部で境界を決めるための基準としてのみ使われます。その起点が TIMESTAMP_NS 型で格納可能な範囲内にある必要はありません。

入力引数から選択された日時型で、指定した月間隔の境界まで切り上げた日時を返します。時刻部分は 00:00:00 に、日部分は 01 に設定されます。

  • <period> が正の数でない場合(≤0)、エラーを返します。
  • いずれかの引数が NULL の場合、NULL を返します。
  • 期間を指定しない場合、デフォルトで 1 か月間隔を使用します。
  • <origin> を指定しない場合、0001-01-01 00:00:00 を起点とします。
  • 入力が DATE 型の場合、デフォルトの時刻は 00:00:00 です。
  • 計算結果が日付の最大範囲である 9999-12-31 23:59:59 を超える場合、エラーを返します。
  • <origin> の日時が <period> より後である場合も、上記の式に従って計算します。このとき、期間数 k は負になります。
  • date_or_time_expr に小数秒の精度がある場合、戻り値にも同じ精度があり、小数部分はゼロになります。
  • 入力が TIMESTAMPTZ 型の場合、まず local_time に変換してから CEIL の計算を行います。たとえば、2025-12-31 23:59:59+05:00 は local_time の 2026-01-01 02:59:59 を表します(セッション変数が +08:00 の場合)。
  • 入力の時刻値(<date_or_time_expr> と <period>)に TIMESTAMPTZ 型と DATETIME 型の両方が含まれる場合、出力は DATETIME 型です。

例​

-- Using default period of 1 month and default origin time 0001-01-01 00:00:00
SELECT MONTH_CEIL('2023-07-13 22:28:18') AS result;
+---------------------+
| result |
+---------------------+
| 2023-08-01 00:00:00 |
+---------------------+

-- Using 5 months as one period, rounding up with default origin point
SELECT MONTH_CEIL('2023-07-13 22:28:18', 5) AS result;
+---------------------+
| result |
+---------------------+
| 2023-12-01 00:00:00 |
+---------------------+

-- If input datetime is exactly at a period starting point, return the input datetime
SELECT MONTH_CEIL('2023-12-01 00:00:00', 5) AS result;
+----------------------------+
| result |
+----------------------------+
| 2023-12-01 00:00:00 |
+----------------------------+

-- Only with origin date and specified date
select month_ceil("2023-07-13 22:28:18", "2022-07-04 00:00:00");
+----------------------------------------------------------+
| month_ceil("2023-07-13 22:28:18", "2022-07-04 00:00:00") |
+----------------------------------------------------------+
| 2023-08-04 00:00:00 |
+----------------------------------------------------------+

-- Specifying origin time
SELECT MONTH_CEIL('2023-07-13 22:28:18', 5, '2023-01-01 00:00:00') AS result;
+---------------------+
| result |
+---------------------+
| 2023-11-01 00:00:00 |
+---------------------+

--- If the <origin> date and time is after the <period>, it will still be calculated according to the above formula, but the period k will be negative
SELECT MONTH_CEIL('2022-09-13 22:28:18', 5, '2028-07-03 22:20:00') AS result;
+---------------------+
| result |
+---------------------+
| 2023-02-03 22:20:00 |
+---------------------+

-- Datetime with scale, time component and decimal places are all truncated to 0
SELECT MONTH_CEIL('2023-07-13 22:28:18.456789', 5) AS result;
+----------------------------+
| result |
+----------------------------+
| 2023-12-01 00:00:00.000000 |
+----------------------------+

-- Input is of DATE type (default time 00:00:00)
SELECT MONTH_CEIL('2023-07-13', 3) AS result;
+---------------------+
| result |
+---------------------+
| 2023-10-01 00:00:00 |
+---------------------+

-- TimeStampTz sample, SET time_zone = '+08:00'
-- Convert to local_time (2026-01-01 02:59:59) and then perform MONTH_CEIL
SELECT MONTH_CEIL('2025-12-31 23:59:59+05:00');
+-----------------------------------------+
| MONTH_CEIL('2025-12-31 23:59:59+05:00') |
+-----------------------------------------+
| 2026-02-01 00:00:00+08:00 |
+-----------------------------------------+

-- If the input time values contain both TIMESTAMPTZ and DATETIME types, the output is DATETIME type.
SELECT MONTH_CEIL('2025-12-31 23:59:59+05:00', '2025-12-15 00:00:00.123');
+--------------------------------------------------------------------+
| MONTH_CEIL('2025-12-31 23:59:59+05:00', '2025-12-15 00:00:00.123') |
+--------------------------------------------------------------------+
| 2026-01-15 00:00:00.123 |
+--------------------------------------------------------------------+

-- Calculation result exceeds maximum date range 9999-12-31, returns error
SELECT MONTH_CEIL('9999-12-13 22:28:18', 5) AS result;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation month_ceil of 9999-12-13 22:28:18, 5 out of range

-- Period is non-positive, returns error
SELECT MONTH_CEIL('2023-07-13 22:28:18', -5) AS result;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation month_ceil of 2023-07-13 22:28:18, -5 out of range

-- Any parameter is NULL, returns NULL
SELECT MONTH_CEIL(NULL, 5), MONTH_CEIL('2023-07-13 22:28:18', NULL) AS result;
+----------------------+--------+
| month_ceil(NULL, 5) | result |
+----------------------+--------+
| NULL | NULL |
+----------------------+--------+