TIME_TRUNC

Updated at:

The TIME_TRUNC function truncates a time value based on the time unit specified by date_part and returns a BIGINT value that represents the time in microseconds.

Syntax

BIGINT TIME_TRUNC(BIGINT <time>, STRING <date_part>)

-- Example.
-- Returns 1234567890.
SELECT TIME_TRUNC(1234567890L,'ff6');

Parameters

  • time: Required. A BIGINT value that represents the time in microseconds. You can use the TO_TIME function to construct this value.

    In the MaxCompute 1.0 data type version, if the parameter is of the STRING or DOUBLE type, it is implicitly converted to the BIGINT type for the calculation.

  • date_part: Required. The time unit for truncation. This parameter must be a STRING constant. Valid values are:

    • MICROSECOND / MICROS / ff6: If you use this granularity, the time value is not truncated because microsecond is the smallest unit of time.

    • MILLISECOND / MILLIS / ff3: Truncates to the nearest millisecond boundary that is less than or equal to the input time. For example, if the time is 15:30:00.123456, the truncated result is 15:30:00.123.

    • SECOND / SS: Truncates to the nearest second boundary that is less than or equal to the input time. For example, if the time is 15:30:00.123456, the truncated result is 15:30:00.

    • MINUTE / MI: Truncates to the nearest minute boundary that is less than or equal to the input time. For example, if the time is 15:30:45.123456, the truncated result is 15:30:00.

    • HOUR / HH: Truncates to the nearest hour boundary that is less than or equal to the input time. For example, if the time is 15:45:30.123456, the truncated result is 15:00:00.

Return value

This function returns a BIGINT value in microseconds. The following rules apply:

  • If time is not a BIGINT value, an error is returned.

  • If date_part is a non-constant value, is in an unsupported format, or is of another data type, an error is returned.

  • If time or date_part is NULL, NULL is returned.

Examples

Examples

  • Example 1: Truncate a time value to the microsecond ('ff6') and millisecond ('ff3') granularities.

    -- Truncate to the microsecond (ff6) granularity. Returns 1234567890.
    SELECT TIME_TRUNC(1234567890, 'ff6');
    
    -- Truncate to the millisecond (ff3) granularity. Returns 1234567000.
    SELECT TIME_TRUNC(1234567890, 'ff3');
  • Example 2: Truncate a time value to the hour ('HOUR'), minute ('MINUTE'), and second ('SECOND') granularities. Then, use the FORMAT_TIME function to convert the returned BIGINT time value to a STRING in the specified format.

    -- Truncate the time '15:31:38.776' to different granularities and format the output.
    -- Truncate to the hour. Returns 15:00:00.
    SELECT FORMAT_TIME(
    TIME_TRUNC(TO_TIME('15:31:38.776', 'HH:MI:SS.ff3'), 'HOUR'),
    '%H:%M:%S'
    ) AS truncated_hour;
    
    -- Truncate to the minute. Returns 15:31:00.
    SELECT FORMAT_TIME(
    TIME_TRUNC(TO_TIME('15:31:38.776', 'HH:MI:SS.ff3'), 'MINUTE'),
    '%H:%M:%S'
    ) AS truncated_minute;
    
    -- Truncate to the second. Returns 15:31:38.
    SELECT FORMAT_TIME(
    TIME_TRUNC(TO_TIME('15:31:38.776', 'HH:MI:SS.ff3'), 'SECOND'),
    '%H:%M:%S'
    ) AS truncated_second;
  • Example 3: Handle NULL inputs.

    -- Returns NULL.
    SELECT TIME_TRUNC(NULL, 'SECOND');
    
    -- If date_part is NULL, NULL is returned.
    SELECT TIME_TRUNC(TO_TIME(12,00,00), NULL);

Related functions

The TIME_TRUNC function is a time function. For more functions that calculate or transform time, see Date and time functions.