SQL EXTRACT vs DATEPART: Date Part Extraction Across Dialects
By Sharon Ben-Moshe · October 7, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Extract year, month, day, and hour across SQL dialects: ANSI EXTRACT, SQL Server DATEPART, MySQL date helpers, and SQLite strftime.
Quick answer
ANSI standard SQL uses EXTRACT(field FROM source) to extract temporal components like YEAR, MONTH, DAY, and HOUR. PostgreSQL, BigQuery, and MySQL support EXTRACT. Microsoft SQL Server uses DATEPART(part, date) or DATENAME(part, date), while SQLite uses strftime('%format', date).
FAQs
Is EXTRACT ANSI SQL compliant?
Yes, EXTRACT is part of the SQL-92 standard and is portable across PostgreSQL, MySQL, Oracle, and Google BigQuery.
What is the difference between DATEPART and DATENAME in SQL Server?
DATEPART returns the date part as an integer (e.g. 5 for May). DATENAME returns the localized name as a string (e.g. 'May' or 'Monday').
How do I extract the day of the week in PostgreSQL?
Use `EXTRACT(DOW FROM timestamp)` where 0 is Sunday, or `EXTRACT(ISODOW FROM timestamp)` where 1 is Monday and 7 is Sunday.
Why does filtering with EXTRACT in WHERE cause slow queries?
Wrapping an indexed timestamp column in `EXTRACT(YEAR FROM created_at) = 2026` is non-SARGable. It forces the database to evaluate the function for every row via a table scan rather than doing an index range seek.
Keep reading
The Comprehensive Guide to SQL Data Types: Storage, Precision, and Cross-Dialect Portability
By Sharon Ben-Moshe · Oct 13, 2026
The complete guide to SQL data types across PostgreSQL, MySQL, SQL Server, and SQLite. Numeric precision, character sets, timestamps, and JSON.
Read postThe Definitive Guide to SQL Window Functions: Syntax, Framing, and Real-World Recipes
By Sharon Ben-Moshe · Oct 13, 2026
The comprehensive guide to SQL window functions. Master OVER, PARTITION BY, ORDER BY, window frames (ROWS vs RANGE), and ranking recipes.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.