Updated on 2026-11-06 GMT+08:00

trunc

The trunc function is used to truncate a date according to a specific format.

Truncation means returning the default value. The default value for year, month, and day is 01. The default value for hour, minute, second, and millisecond is 00.

Syntax

trunc(string date, string format)

Parameters

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

date

Yes

DATE or STRING

The date to be processed.

Formats:
  • yyyy-mm-dd
  • yyyy-mm-dd hh:mi:ss
  • yyyy-mm-dd hh:mi:ss.ff3

format

Yes

STRING

The target date format for conversion.

The format is a combination of time units representing year, month, day, hour, minute, and second, along with any characters.

  • yyyy indicates year.
  • MM indicates month.

Return Values

Returns a date value of type DATE, in the format yyyy-mm-dd.

  • If date is not of type DATE or STRING, an error is returned with the message "data type mismatch".
  • If date is of type DATE or STRING but does not conform to the input format for date values, NULL is returned.
  • If the date value is NULL, NULL is returned.
  • If the format value is NULL, NULL is returned.

Example Code

Returns 2023-08-01.

select trunc('2023-08-16', 'MM');

Returns 2023-08-01.

select trunc('2023-08-16 10:54:36', 'MM');

Returns NULL.

 select trunc(null, 'MM');