Extract a specific component (year, month, day, hour, etc.) from a date or timestamp as a numeric value.
Pulls a specific component (year, month, day, hour, etc.) from a date or timestamp value. EXTRACT is ANSI SQL standard; DATE_PART is PostgreSQL-specific. Both are essential for grouping by time periods or parsing temporal data.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | all | EXTRACT(unit FROM date) is SQL-standard and supported. Also YEAR(date), MONTH(date), DAY(date), HOUR(time), MINUTE(time), SECOND(time) as shorthand functions. DAYOFWEEK() returns 1=Sunday, WEEKDAY() returns 0=Monday. |
| PostgreSQL | ✓ Supported | all | EXTRACT(field FROM source) returns a float8. DATE_PART(field, source) is equivalent. Fields include: year, month, day, hour, minute, second, milliseconds, epoch, dow (0=Sun), isodow (1=Mon), week, quarter, timezone. |
| SQL Server | ✓ Supported | all | DATEPART(datepart, date) returns an integer. YEAR(date), MONTH(date), DAY(date) are shorthand. Parts include: year, quarter, month, dayofyear, day, weekday, week, hour, minute, second, millisecond, microsecond. DATEPART(weekday, date) is locale-dependent (SET DATEFIRST). |
| Oracle | ✓ Supported | all | EXTRACT(field FROM date). Fields: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, TIMEZONE_HOUR, TIMEZONE_MINUTE. TO_CHAR(date, 'format') is the traditional Oracle approach for formatting. |
| SQLite | ✗ Not Supported | — | No EXTRACT() or DATEPART(). Use strftime() with format codes: %Y=year, %m=month, %d=day, %H=hour, %M=minute, %S=second, %w=weekday (0=Sun), %W=week number. |
SQL Server uses DATEPART() instead of EXTRACT() — they're equivalent but different names. PostgreSQL's EXTRACT(EPOCH FROM ts) to get a Unix timestamp is uniquely useful. SQLite has no EXTRACT at all; strftime() with a CAST is the workaround. Week-of-year and day-of-week numbering differs between engines (ISO vs locale-based) — always test if you're using those.
EXTRACT(unit FROM date) is SQL-standard and supported. Also YEAR(date), MONTH(date), DAY(date), HOUR(time), MINUTE(time), SECOND(time) as shorthand functions. DAYOFWEEK() returns 1=Sunday, WEEKDAY() returns 0=Monday.
EXTRACT(field FROM source) returns a float8. DATE_PART(field, source) is equivalent. Fields include: year, month, day, hour, minute, second, milliseconds, epoch, dow (0=Sun), isodow (1=Mon), week, quarter, timezone.
DATEPART(datepart, date) returns an integer. YEAR(date), MONTH(date), DAY(date) are shorthand. Parts include: year, quarter, month, dayofyear, day, weekday, week, hour, minute, second, millisecond, microsecond. DATEPART(weekday, date) is locale-dependent (SET DATEFIRST).
EXTRACT(field FROM date). Fields: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, TIMEZONE_HOUR, TIMEZONE_MINUTE. TO_CHAR(date, 'format') is the traditional Oracle approach for formatting.
No EXTRACT() or DATEPART(). Use strftime() with format codes: %Y=year, %m=month, %d=day, %H=hour, %M=minute, %S=second, %w=weekday (0=Sun), %W=week number.