SQL DATE_PART
A Postgres function that does exactly what EXTRACT does — same result, function-call syntax with the field name as a text string.
What & Why
DATE_PART('field', source) is functionally identical to EXTRACT(field FROM source) — the only difference is syntax. DATE_PART takes the field name as a quoted text string and uses regular function-call parentheses; EXTRACT uses its own special FROM-based syntax.
See How It Works
BUSINESS QUESTION
Marketing wants lead volume by calendar month.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
EXAMPLE QUERY
SELECT
DATE_PART('month', created_at)::int AS lead_month,
COUNT(*) AS leads
FROM marketing.leads
GROUP BY lead_month
ORDER BY lead_month;RESULT — exact output from the displayed Queryflo rows
| lead_month | leads |
|---|---|
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
DATE_PART groups the four lead timestamps into January, February, and March.
Now You Try
Practice this concept
Marketing wants lead volume by calendar month.
Available schema
marketingPrefix tables with marketing.table_name.
lead_monthleadsmarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario