Skip to main content
Truncates a time value based on the specified date part, such as year, day, hour, or minute. The database also provides the year, quarter, month, week, day, and hour functions for you to extract the specified date part.

Syntax

Parameters

  • datetime: the time to truncate, which can be of the DATETIME or DATE type. The date and time must exist. Otherwise, NULL will be returned. For example, 2021-02-29 11:12:13 does not exist as a date and NULL will be returned.
  • fmt: the date part, that is, to which precision datetime will be truncated. The value must be a VARCHAR constant. fmt must be set to a value listed in the following table. If the value is incorrect, an error will be returned. If datetime is a DATE value, fmt can only be year, quarter, month, week, or day. If you set fmt to other date units, for example hour, an error is reported. See Example 8.

Return value

Returns a value of the DATETIME or DATE type. If datetime is of the DATE type and fmt is set to hour, minute, or second, the time part of the returned value defaults to 00:00:00.

Examples

Example 1: Truncate the input time to the microsecond.
Example 2: Truncate the input time to the millisecond.
Example 3: Truncate the input time to the minute.
Example 4: Truncate the input time to the hour.
Example 5: Truncate the input time to the first day of a week.
Example 6: Truncate the input time to the first day of a quarter.
Example 7: Truncate the input time to the first day of a year.
Example 8: Truncate a DATE value to the hour. An error is returned.
Example 9: Truncate the input date to the first day of a year.