DATE_TRUNC
更新时间:2025-10-17
描述
将 datetime 按照指定的时间单位截断。
语法
SQL
1DATE_TRUNC(<datetime>, <time_unit>)
2DATE_TRUNC(<time_unit>, <datetime>)
参数
| 参数 | 说明 |
|---|---|
<datetime> |
合法的日期表达式 |
<time_unit> |
希望截断的时间间隔,可选的值如下:[second,minute,hour,day,week,month,quarter,year] |
返回值
按照指定的时间单位截断的时间
举例
SQL
1select date_trunc('2010-12-02 19:28:30', 'second');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'second') |
3+-------------------------------------------------+
4| 2010-12-02 19:28:30 |
5+-------------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'minute');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'minute') |
3+-------------------------------------------------+
4| 2010-12-02 19:28:00 |
5+-------------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'hour');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'hour') |
3+-------------------------------------------------+
4| 2010-12-02 19:00:00 |
5+-------------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'day');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'day') |
3+-------------------------------------------------+
4| 2010-12-02 00:00:00 |
5+-------------------------------------------------+
SQL
1select date_trunc('2023-4-05 19:28:30', 'week');
Text
1+-------------------------------------------+
2| date_trunc('2023-04-05 19:28:30', 'week') |
3+-------------------------------------------+
4| 2023-04-03 00:00:00 |
5+-------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'month');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'month') |
3+-------------------------------------------------+
4| 2010-12-01 00:00:00 |
5+-------------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'quarter');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'quarter') |
3+-------------------------------------------------+
4| 2010-10-01 00:00:00 |
5+-------------------------------------------------+
SQL
1select date_trunc('2010-12-02 19:28:30', 'year');
Text
1+-------------------------------------------------+
2| date_trunc('2010-12-02 19:28:30', 'year') |
3+-------------------------------------------------+
4| 2010-01-01 00:00:00 |
5+-------------------------------------------------+
