TRUNC

Syntax

TRUNC
(
  expr  IN date [, fmt   IN text]
)
RETURNS date;

TRUNC
(
  expr  IN timestamp without time zone [, fmt   IN text]
)
RETURNS timestamp without time zone;

TRUNC
(
  expr  IN timestamp with time zone [, fmt   IN text]
)
RETURNS timestamp with time zone;

Overview

The TRUNC function truncates subunits from a date or timestamp value and returns the truncated (discarded) result to fit the specified format model.

In other words, it removes the decimal part (or detail units) from the time or date part of the value, leaving only the desired units (e.g., year, month, day, etc.).

If no format string (fmt) is provided, it defaults to 'DDD' (day in days out).

For the timestamp version, the internal time information is processed together to truncate sub-hour increments, and sub-second information is set to zero.

Parameters

Parameter
Description

expr

date, timestamp, timestamptz type; the target date or timestamp value to truncate. If this value is NULL, the result will also be NULL.

fmt

text type;

A format string that specifies which units the date or timestamp value should be truncated to. For example, you can specify ‘YYYY’ (year), ‘MM’ (month), ‘DDD’ (day), etc.

If this value is NULL, the default value (e.g., ‘DDD’) is used.

Example

Last updated