MONTH_CEIL
説明
month_ceil関数は、入力されたdatetime値を指定された月間隔の最も近い上位の値に切り上げます。originが指定されている場合は、それをベースラインとして使用します。そうでない場合は、0001-01-01 00:00:00がデフォルトとして使用されます。
日付計算式:
は、ベースライン時刻から対象時刻に到達するために必要な期間数を表します。
構文
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> | 切り上げ対象のdatetime値。date/datetime/timestamptz型をサポートする有効な日付式です。Date型は対応する日付の開始時刻00:00:00に変換されます。具体的な形式については、timestamptz conversion、datetime conversion、date conversionを参照してください。 |
<period> | 月間隔値。INT型で、各間隔に含まれる月数を表します。 |
<origin> | 間隔の開始時点。date/datetime型をサポートします。デフォルト値は0001-01-01 00:00:00です。 |
戻り値
DATETIME型の値を返します。入力されたdatetimeに基づいて、指定された月間隔の最も近い値に切り上げられた時刻値を表します。結果の時刻部分は00:00:00に設定され、日部分は01に切り捨てられます。
<period>が非正数(≤0)の場合、エラーを返します。- いずれかのパラメータがNULLの場合、NULLを返します。
- periodが指定されていない場合、デフォルトで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 に変換され(例:セッション変数が
+08:00の場合、2025-12-31 23:59:59+05:00はlocal_time2026-01-01 02:59:59を表します)、その後CEIL計算が実行されます。 - 入力時刻値(
<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 |
+----------------------+--------+