原文地址:http://impala.apache.org/docs/build/html/topics/impala_datetime_functions.html
TRUNC(TIMESTAMP / DATE ts, STRING unit)
Purpose: Returns the ts truncated(截断的) to the unit specified.
Argument: The unit argument is not case-sensitive. This argument string can be one of:
Unit | Supported for TIMESTAMP ts | Suppport for DATE ts |
Description |
---|---|---|---|
SYYYY / YYYY / YEAR SYEAR / YYY / YY / Y |
Y | Y | Year |
Q | Y | Y | Quarter(季度) |
MONTH / MON MM / RM |
Y | Y | Month |
WW | Y | Y | The most recent date that is the same day of the week as the first day of the year |
W | Y | Y | The most recent date that is the same day of the week as the first day of the month |
DDD / DD / J | Y | Y | Day |
DAY / DY / D | Y | Y | Starting day of the week (Monday) |
HH / HH12 / HH24 | Y | N | Hour. A TIMESTAMP value truncated to the hour is always represented in 24-hour notation, even for the HH12 argument string. |
MI | Y | N | Minute |
Added in: The ability to truncate numeric values is new starting in Impala 2.10.
Usage notes:
The TIMESTAMP
form is typically used in GROUP BY
queries to aggregate results from the same hour, day, week, month, quarter, and so on. You can also use this function in an INSERT ... SELECT
into a partitioned table to divide TIMESTAMP
values into the correct partition.
Return type:
TIMESTAMP
if the first argument, ts, isTIMESTAMP
.DATE
if the first argument, ts, isDATE
.
Example:
SELECT TRUNC(DATE'2019-05-08','YEAR') -- 2019-01-01
SELECT TRUNC(DATE'2019-05-08', 'QUARTER') -- 2019-04-01
--每月1号
SELECT TRUNC(NOW(), 'MM') -- 2021-04-01 00:00:00