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.

Kindfunction
Example LanguageSQL

📝 Syntax

extract(field text from source timestamp) → numeric

⚙️ Parameters

field textThe field to extract: century, decade, year, quarter, month, week, day, doy, dow, isodow, hour, minute, second, milliseconds, epoch or julian. Type text.
source timestampThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏