メインコンテンツまでスキップ
バージョン: 26.x

DAY_FLOOR

説明

DAY_FLOOR関数は、指定された日付または時刻の値を、指定された日の期間の開始時点まで切り下げるために使用されます。この関数は、入力された日付と時刻以下の最大の期間の瞬間を返します。期間のルールは、period(期間内の日数)とorigin(開始基準時刻)によって共同で定義されます。開始基準時刻が指定されない場合、計算基準として0001-01-01 00:00:00がデフォルトとなります。

日付計算式:

$$ \text{DAY_FLOOR}(\langle\text{date_or_time_expr}\rangle, \langle\text{period}\rangle, \langle\text{origin}\rangle) = \max{\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} \leq \langle\text{date_or_time_expr}\rangle} $$ $k$は基準時刻から対象時刻までの期間数を表します。

構文

DAY_FLOOR(<date_or_time_expr>)
DAY_FLOOR(<date_or_time_expr>, <origin>)
DAY_FLOOR(<date_or_time_expr>, <period>)
DAY_FLOOR(<date_or_time_expr>, <period>, <origin>)

パラメータ

パラメータ説明
<date_or_time_expr>date/datetime型をサポートする有効な日付式。特定のdatetimeおよびdate形式については、datetime conversionおよびdate conversionを参照してください
<period>各期間の日数を指定します。INT型です。負数または0の場合はNULLを返します。指定されない場合、デフォルト期間は1日です。
<origin>期間計算の開始基準時刻。date/datetime型をサポートします

戻り値

入力値を指定された日期間に切り下げた結果を表す日付または時刻値を返します。

入力が有効な場合、datetime型と一致する切り下げ結果を返します:

入力がDATEの場合、DATEを返します 入力がDATETIMEの場合、DATETIMEを返します(日付と時刻を含み、期間が日単位のため時刻部分は00:00:00になります)。

特殊なケース:

  • いずれかのパラメータがNULLの場合、NULLを返します
  • periodが負数または0の場合、エラーを返します
  • スケール付きのdatetime入力の場合、出力はすべての小数点以下が0のスケール付きになります
  • <origin>の日時が<period>より後の場合でも、上記の式に従って計算されますが、期間kは負数になります。

-- Round down with a period of five days
select day_floor("2023-07-13 22:28:18", 5);

+-------------------------------------+
| day_floor("2023-07-13 22:28:18", 5) |
+-------------------------------------+
| 2023-07-10 00:00:00 |
+-------------------------------------+


-- Datetime input with scale, return value has scale with all decimals as 0
mysql> select day_floor("2023-07-13 22:28:18.123", 5);
+-----------------------------------------+
| day_floor("2023-07-13 22:28:18.123", 5) |
+-----------------------------------------+
| 2023-07-10 00:00:00.000 |
+-----------------------------------------+


-- Input parameter without period, default to one day as period
select day_floor("2023-07-13 22:28:18");
+----------------------------------+
| day_floor("2023-07-13 22:28:18") |
+----------------------------------+
| 2023-07-13 00:00:00 |
+----------------------------------+

-- Only with origin date and specified date
select day_floor("2023-07-13 22:28:18", "2023-01-01 12:00:00");
+---------------------------------------------------------+
| day_floor("2023-07-13 22:28:18", "2023-01-01 12:00:00") |
+---------------------------------------------------------+
| 2023-07-13 12:00:00 |
+---------------------------------------------------------+

-- Specify period as 7 days (1 week), custom reference time as 2023-01-01 00:00:00
select day_floor("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00");
+------------------------------------------------------------+
| day_floor("2023-07-13 22:28:18", 7, "2023-01-01 00:00:00") |
+------------------------------------------------------------+
| 2023-07-09 00:00:00 |
+------------------------------------------------------------+

-- Start time is exactly at the beginning of a period, returns input date time
select day_floor("2023-07-09 00:00:00", 7, "2023-01-01 00:00:00");
+------------------------------------------------------------+
| day_floor("2023-07-09 00:00:00", 7, "2023-01-01 00:00:00") |
+------------------------------------------------------------+
| 2023-07-09 00:00:00 |
+------------------------------------------------------------+

-- Input is DATE type, period is 3 days
select day_floor(cast("2023-07-13" as date), 3);
+------------------------------------------+
| day_floor(cast("2023-07-13" as date), 3) |
+------------------------------------------+
| 2023-07-11 |
+------------------------------------------+

-- Period is negative, returns error
select day_floor("2023-07-13 22:28:18", -2);
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.3)[E-218]Operation day_floor of 2023-07-13 22:28:18, -2 out of range

--- 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_floor('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00');
+----------------------------------------------------------------+
| day_floor('2023-07-13 19:30:00.123', 4, '2028-07-14 08:00:00') |
+----------------------------------------------------------------+
| 2023-07-13 08:00:00.000 |
+----------------------------------------------------------------+

-- Any parameter is NULL, returns NULL
select day_floor(NULL, 5, "2023-01-01");
+----------------------------------+
| day_floor(NULL, 5, "2023-01-01") |
+----------------------------------+
| NULL |
+----------------------------------+