に最終更新
DATE_TRUNC
説明
DATE_TRUNC 関数は、日付または時刻の値(datetime)を指定した時間単位(time_unit)に切り詰めます。指定した単位以上の時間情報を保持し、それより下位の時間情報を最小値にリセットします。たとえば、「hour」単位に切り詰めると、年、月、日、時は保持され、分や秒などはゼロになります。年単位に切り詰めると、月と日は切り詰められ、結果は xxxx-01-01 の形式になります。
構文
DATE_TRUNC(<datetime>, <time_unit>)
DATE_TRUNC(<time_unit>, <datetime>)
パラメータ
| パラメータ | 説明 |
|---|---|
<datetime> | 有効な日付式。DATETIME 型または DATE 型をサポートします。具体的な形式については、TIMESTAMPTZ の変換、DATETIME の変換、DATE の変換を参照してください。 |
<time_unit> | 切り詰める時間単位。指定できる値は [second,minute,hour,day,week,month,quarter,year] です。時間単位は文字列定数である必要があり、列や定数でない式は指定できません。 |
戻り値
切り詰めた結果を、datetime に応じた型で返します。
- 入力が DATE 型の場合は、DATE 型を返します。
- 入力が DATETIME 型または時刻を含む文字列の場合は、DATETIME 型(日付と切り詰めた時刻を含む)を返します。
- 入力が TIMESTAMPTZ 型の場合は、まず local_time に変換してから切り詰めます。たとえば、セッション変数が
+08:00の場合、2025-12-31 23:59:59+05:00が表す local_time は2026-01-01 02:59:59です。 - 小数秒の精度を持つ DATETIME 型では、小数秒部分はゼロに切り詰められますが、戻り値の精度は保持されます。
特殊なケース:
- いずれかのパラメータが NULL の場合は、NULL を返します。
- time_unit がサポートされていない場合は、エラーを返します。
例
-- Truncate by second, minute, hour, day, week, month, quarter, year
mysql> select date_trunc(cast('2010-12-02 19:28:30' as datetime), 'second');
+---------------------------------------------------------------+
| date_trunc(cast('2010-12-02 19:28:30' as datetime), 'second') |
+---------------------------------------------------------------+
| 2010-12-02 19:28:30 |
+---------------------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'minute');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'minute') |
+-------------------------------------------------+
| 2010-12-02 19:28:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'hour');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'hour') |
+-------------------------------------------------+
| 2010-12-02 19:00:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'day');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'day') |
+-------------------------------------------------+
| 2010-12-02 00:00:00 |
+-------------------------------------------------+
select date_trunc('2023-4-05 19:28:30', 'week');
+-------------------------------------------+
| date_trunc('2023-04-05 19:28:30', 'week') |
+-------------------------------------------+
| 2023-04-03 00:00:00 |
+-------------------------------------------+
select date_trunc(cast('2010-12-02' as date), 'month');
+-------------------------------------------------+
| date_trunc(cast('2010-12-02' as date), 'month') |
+-------------------------------------------------+
| 2010-12-01 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'quarter');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'quarter') |
+-------------------------------------------------+
| 2010-10-01 00:00:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'year');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'year') |
+-------------------------------------------------+
| 2010-01-01 00:00:00 |
+-------------------------------------------------+
-- For datetime with scale, fractional digits will be truncated to zero without rounding, but return value retains scale
mysql> select date_trunc('2010-12-02 19:28:30.523', 'second');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30.523', 'second') |
+-------------------------------------------------+
| 2010-12-02 19:28:30.000 |
+-------------------------------------------------+
-- Example of TimeStampTz type, SET time_zone = '+08:00'
-- Convert the variable value to local_time(2026-01-01 02:59:59) before truncation
SELECT DATE_TRUNC('2025-12-31 23:59:59+05:00', 'year');
+-------------------------------------------------+
| DATE_TRUNC('2025-12-31 23:59:59+05:00', 'year') |
+-------------------------------------------------+
| 2026-01-01 00:00:00+08:00 |
+-------------------------------------------------+
-- Unsupported unit, returns error
select date_trunc('2010-12-02 19:28:30', 'quar');
ERROR 1105 (HY000): errCode = 2, detailMessage = date_trunc function time unit param only support argument is year|quarter|month|week|day|hour|minute|second