Dates and timesPostgreSQL only

DATE_PART

Pulls a single part out of a date or time — the same as EXTRACT, but as a regular function.

Syntax

DATE_PART('<field>', <date or time>)

Returns the year, month, day, hour and so on. It does the same as EXTRACT, except the field is written in quotes as the first argument: DATE_PART('month', created_at). The difference is the type: DATE_PART returns double precision, while EXTRACT returns numeric.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

Year and hour
SELECT DATE_PART('year', TIMESTAMP '2026-09-13 15:47') AS year,
DATE_PART('hour', TIMESTAMP '2026-09-13 15:47') AS hour;
yearhour
202615
The month of each order
SELECT id, created_at,
DATE_PART('month', created_at) AS month
FROM orders;
idcreated_atmonth
12026-07-03 10:15:007
22026-07-28 18:40:007
32026-08-09 09:05:008
42026-08-21 14:30:008
52026-09-02 11:00:009
How many seconds in an interval
SELECT DATE_PART('epoch', INTERVAL '2 hours') AS seconds;
seconds
7200

For an interval, 'epoch' is its length in seconds.

Watch out

The field goes in quotes: DATE_PART(month, created_at) without quotes throws column "month" does not exist, because PostgreSQL looks for a column.

It returns a number, not a name: DATE_PART('month', …) gives 9, not September. The name comes from TO_CHAR(created_at, 'FMMonth').

Like EXTRACT, it's no good for grouping by month: September of different years merges together. Use DATE_TRUNC.

Related functions