跳到主要内容
版本:Latest-3.2

last_day

功能

根据指定的时间单位,返回输入的日期对应的最后一天。比如 last_day('2023-05-10', 'month') 返回 '2023-05-10' 所在月份的最后一天。

如果没有指定时间单位,则返回该日期对应月份的最后一天。

该函数从 3.1 版本开始支持。

语法

DATE last_day(DATETIME|DATE date_expr[, VARCHAR unit])

参数说明

  • date_expr:DATE 或 DATETIME 表达式,必填。
  • unit: 时间单位,可选。有效值包括 month, quarter, 和 year。如果不填,默认为 month。如果输入的 unit 不合法,返回报错。

返回值说明

返回一个日期值。

示例

MySQL > select last_day('2023-05-10', 'month');
+----------------------------------+
| last_day('2023-05-10', 'month') |
+----------------------------------+
| 2023-05-31 |
+----------------------------------+

MySQL > select last_day('2023-05-10');
+------------------------+
| last_day('2023-05-10') |
+------------------------+
| 2023-05-31 |
+------------------------+

MySQL > select last_day('2023-05-10', 'quarter');
+-----------------------------------+
| last_day('2023-05-10', 'quarter') |
+-----------------------------------+
| 2023-06-30 |
+-----------------------------------+

MySQL > select last_day('2023-05-10', 'year');
+---------------------------------------+
| last_day('2023-05-10', 'year') |
+---------------------------------------+
| 2023-12-31 |
+---------------------------------------+

keyword

LAST_DAY, LAST