📣
TiDB Cloud Premium is now in public preview. Unlimited growth, instant elasticity, advanced security for enterprise workloads. Try it out →

DATE_PART



Retrieves the designated portion of a date or timestamp.

See also: EXTRACT

Syntax

DATE_PART( YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND | DOW | DOY | EPOCH | ISODOW | YEARWEEK | MILLENNIUM, <date_or_timestamp_expr> )
KeywordDescription
DOWDay of the Week. Sunday (0) through Saturday (6).
DOYDay of the Year. 1 through 366.
EPOCHThe number of seconds since 1970-01-01 00:00:00.
ISODOWISO Day of the Week. Monday (1) through Sunday (7).
YEARWEEKThe year and week number combined, following ISO 8601 (e.g., 202415).
MILLENNIUMThe millennium of the date (1 for years 1–1000, 2 for 1001–2000, etc.).

Return Type

Integer.

Examples

This example demonstrates how to use DATE_PART to extract various components—such as year, month, ISO week day, year-week combination, and millennium—from the current timestamp:

SELECT DATE_PART(YEAR, NOW()) AS year_part, DATE_PART(QUARTER, NOW()) AS quarter_part, DATE_PART(MONTH, NOW()) AS month_part, DATE_PART(WEEK, NOW()) AS week_part, DATE_PART(DAY, NOW()) AS day_part, DATE_PART(HOUR, NOW()) AS hour_part, DATE_PART(MINUTE, NOW()) AS minute_part, DATE_PART(SECOND, NOW()) AS second_part, DATE_PART(DOW, NOW()) AS dow_part, DATE_PART(DOY, NOW()) AS doy_part, DATE_PART(EPOCH, NOW()) AS epoch_part, DATE_PART(ISODOW, NOW()) AS isodow_part, DATE_PART(YEARWEEK, NOW()) AS yearweek_part, DATE_PART(MILLENNIUM, NOW()) AS millennium_part;
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ year_part │ quarter_part │ month_part │ week_part │ day_part │ hour_part │ minute_part │ second_part │ dow_part │ doy_part │ epoch_part │ isodow_part │ yearweek_part │ millennium_part │ ├───────────┼──────────────┼────────────┼───────────┼──────────┼───────────┼─────────────┼─────────────┼──────────┼──────────┼───────────────────┼─────────────┼───────────────┼─────────────────┤ │ 202524161618101031061744827010.25767132025163 │ └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Was this page helpful?