date_part function

Extracts a part of the date, timestamp, or interval.

Syntax

date_part(field, expr)

Arguments

  • field: An STRING literal. See extract function for details.

  • expr: A DATE, TIMESTAMP, or INTERVAL expression.

Returns

If field is ‘SECOND’, a DECIMAL(8, 6). In all other cases, an INTEGER.

The date_part function is a synonym for extract(field FROM expr).

Examples

> SELECT date_part('YEAR', TIMESTAMP'2019-08-12 01:00:00.123456');
 2019
> SELECT date_part('WEEK', TIMESTAMP'2019-08-12 01:00:00.123456');
 33
> SELECT date_part('DAY', DATE'2019-08-12');
 224
> SELECT date_part('SECONDS', TIMESTAMP'2019-10-01 00:00:01.000001');
 1.000001
> SELECT date_part('MONTHS', INTERVAL '2-11' YEAR TO MONTH);
 11
> SELECT date_part('SECONDS', INTERVAL '5:00:30.001' HOUR TO SECOND);
 30.001000