DAY_CEIL
概要
day_ceil関数は、指定された日付または時間値を、指定された日周期の開始時点まで切り上げます。周期ルールはperiod(日数)とorigin(基準時刻)によって定義されます。originが指定されていない場合、デフォルトで0001-01-01 00:00:00になります。
日付計算式: $$ \begin{aligned} &\text{day_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{day} \mid \ &k \in \mathbb{Z} \land \langle\text{origin}\rangle + k \times \langle\text{period}\rangle \times \text{day} \geq \langle\text{date_or_time_expr}\rangle} \end{aligned} $$ $k$は基準時刻から目標時刻に到達するために必要な周期数を表します。
構文
DAY_CEIL(<date_or_time_expr>)
DAY_CEIL(<date_or_time_expr>, <origin>)
DAY_CEIL(<date_or_time_expr>, <period>)
DAY_CEIL(<date_or_time_expr>, <period>, <origin>)
パラメータ
| Parameter | デスクリプション |
|---|---|
<date_or_time_expr> | date/datetimeタイプをサポートする有効な日付式。特定のdatetimeおよびdate形式については、datetime conversionおよびdate conversionを参照してください |
<period> | 各期間の日数を指定します。INTタイプです。指定されない場合、デフォルトの期間は1日です。 |
<origin> | 期間計算の開始基準時刻。date/datetimeタイプをサポートします |
Return Value
入力値を指定された日期間に切り上げた結果を表す日付または時刻の値を返します。
入力が有効な場合、datetimeタイプと一致する丸め結果を返します:
入力がDATEの場合、DATEを返します; 入力がDATETIMEの場合、DATETIMEを返します(日付と時刻を含み、時刻部分は00:00:00になります。期間は日単位のため)。
特殊なケース:
- パラメータのいずれかがNULLの場合、NULLを返します;
- periodが負の値または0の場合、エラーを返します;
- 丸め結果が日付タイプのサポート範囲を超える場合('9999-12-31'以降など)、エラーが報告されます。
- スケールを持つdatetime入力の場合、出力はすべての小数点以下を0に切り捨て、戻り値にスケールが含まれます
<origin>の日付と時刻が<period>より後の場合でも、上記の公式に従って計算されますが、期間kは負の値になります。
Examples
-- Round up with a period of five days
select day_ceil( cast("2023-07-13 22:28:18" as datetime), 5);
+------------------------------------+
| day_ceil("2023-07-13 22:28:18", 5) |
+------------------------------------+
| 2023-07-15 00:00:00 |
+------------------------------------+
-- Datetime input with scale, return value has scale with all decimals as 0
select day_ceil( "2023-07-13 22:28:18.123", 5);
+-----------------------------------------+
| day_ceil( "2023-07-13 22:28:18.123", 5) |
+-----------------------------------------+
| 2023-07-15 00:00:00.000 |
+-----------------------------------------+
-- Without specifying period, default to round up by one day
select day_ceil("2023-07-13 22:28:18");
+---------------------------------+
| day_ceil("2023-07-13 22:28:18") |
+---------------------------------+
| 2023-07-14 00:00:00 |
+---------------------------------+
-- Only with origin date and specified date
select day_ceil("2023-07-13 22:28:18", "2021-07-01 12:22:34");
+--------------------------------------------------------+
| day_ceil("2023-07-13 22:28:18", "2021-07-01 12:22:34") |
+--------------------------------------------------------+
| 2023-07-14 12:22:34 |
+--------------------------------------------------------+
-- Specify period as 7 days (1 week), custom reference time as 2023-01-01 00:00:00
select day_ceil("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00");
+-----------------------------------------------------------+
| day_ceil("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00") |
+-----------------------------------------------------------+
| 2023-07-16 00:00:00 |
+-----------------------------------------------------------+
-- Date and time is exactly at the start of the period
select day_ceil("2023-07-16 00:00:00", 7, "2023-01-01 00:00:00");
+-----------------------------------------------------------+
| day_ceil("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00") |
+-----------------------------------------------------------+
| 2023-07-16 00:00:00 |
+-----------------------------------------------------------+
-- Input is DATE type, period is 3 days
select day_ceil(cast("2023-07-13" as date), 3);
+-----------------------------------------+
| day_ceil(cast("2023-07-13" as date), 3) |
+-----------------------------------------+
| 2023-07-14 |
+-----------------------------------------+
--- 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 day_ceil('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00');
+---------------------------------------------------------------+
| day_ceil('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00') |
+---------------------------------------------------------------+
| 2023-07-17 08:00:00.000 |
+---------------------------------------------------------------+
-- Period time is zero, returns NULL
select day_ceil(cast("2023-07-13" as date), 0);
+-----------------------------------------+
| day_ceil(cast("2023-07-13" as date), 0) |
+-----------------------------------------+
| NULL |
+-----------------------------------------+
-- Period is negative
mysql> select day_ceil("2023-07-13 22:28:18", -2);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation day_ceil of 2023-07-13 22:28:18, -2 out of range
-- Return date exceeds maximum range, returns error
select day_ceil("9999-12-31", 5);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation day_ceil of 9999-12-31 00:00:00, 5 out of range
-- Any parameter is NULL, returns NULL
select day_ceil(NULL, 5, "2023-01-01");
+---------------------------------+
| day_ceil(NULL, 5, "2023-01-01") |
+---------------------------------+
| NULL |
+---------------------------------+