extract(field from source) — Extract a date part from a timestamp
Last updated: 2026-11-04
extract(field from source) — Extract a date part from a timestamp
extract() pulls an individual part out of a date or timestamp — year, month, day, hour, and also the day of week or the quarter. The result is always numeric, which makes it convenient for arithmetic; date_part() is the equivalent function if you prefer naming the field as a string argument.
| Kind | function |
|---|---|
| Example Language | SQL |
📝 Syntax
extract(field text from source timestamp) → numeric
⚙️ Parameters
| field text | The field to extract: century, decade, year, quarter, month, week, day, doy, dow, isodow, hour, minute, second, milliseconds, epoch or julian. Type text. |
|---|---|
| source timestamp | The source date or timestamp: a date, timestamp or timestamptz. |
Returns:numeric — the extracted field. It comes back as numeric rather than integer, so it can be used in calculations directly.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
id | sold_on | sale_year | sale_month | sale_day | day_of_week
----+------------+-----------+------------+----------+-------------
1 | 2024-03-15 | 2024 | 3 | 15 | 5
2 | 2024-07-01 | 2024 | 7 | 1 | 1
3 | 2024-12-31 | 2024 | 12 | 31 | 2
(3 rows)
📌 Key Points
- The field name in EXTRACT(YEAR FROM ...) is case insensitive — YEAR, year and Year all behave the same.
- DOW returns 0-6 with 0 meaning Sunday; for the ISO 8601 numbering use ISODOW, where 1 is Monday.
- extract() returns numeric, so cast with ::int when you need a whole number.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhat is the difference between extract() and date_part()?
AThey are the same function: date_part('year', ts) takes the field as a string argument, while extract(YEAR FROM ts) uses the SQL standard syntax.