Date Part Functions
The date_part, date_trunc and date_diff functions can be used to extract or manipulate parts of temporal types such as TIMESTAMP, TIMESTAMPTZ, DATE and INTERVAL.
The parts to be extracted or manipulated are specified by one of the strings in the tables below.
The example column provides the corresponding parts of the timestamp 2021-08-03 11:59:44.123456.
Only the entries of the first table can be extracted from INTERVALs or used to construct them.
Part Specifiers Usable as Date Part Specifiers and in Intervals
| Specifier | Description | Synonyms | Example |
|---|---|---|---|
century | Gregorian century | cent, centuries, c | 21 |
day | Gregorian day | days, d, dayofmonth | 3 |
decade | Gregorian decade | dec, decades, decs | 202 |
hour | Hours | hr, hours, hrs, h | 11 |
microseconds | Sub-minute microseconds | microsecond, us, usec, usecs, usecond, useconds | 44123456 |
millennium | Gregorian millennium | mil, millenniums, millenia, mils, millenium | 3 |
milliseconds | Sub-minute milliseconds | millisecond, ms, msec, msecs, msecond, mseconds | 44123 |
minute | Minutes | min, minutes, mins, m | 59 |
month | Gregorian month | mon, months, mons | 8 |
quarter | Quarter of the year (1-4) | quarters | 3 |
second | Seconds | sec, seconds, secs, s | 44 |
year | Gregorian year | yr, y, years, yrs | 2021 |
Part Specifiers Only Usable as Date Part Specifiers
| Specifier | Description | Synonyms | Example |
|---|---|---|---|
dayofweek | Day of the week (Sunday = 0, Saturday = 6) | weekday, dow | 2 |
dayofyear | Day of the year (1-365/366) | doy | 215 |
epoch | Seconds since 1970-01-01 | 1627991984.123456 | |
era | Gregorian era (CE/AD, BCE/BC) | 1 | |
isodow | ISO day of the week (Monday = 1, Sunday = 7) | 2 | |
isoyear | ISO Year number (Starts on Monday of week containing Jan 4th) | 2021 | |
julian | Julian Day number. | 2459430.4998162435 | |
timezone_hour | Time zone offset hour portion | 0 | |
timezone_minute | Time zone offset minute portion | 0 | |
timezone | Time zone offset in seconds | 0 | |
week | Week number | weeks, w | 31 |
yearweek | ISO year and week number in YYYYWW format | 202131 |
Part Functions
There are dedicated extraction functions to get certain subfields:
| Name | Description |
|---|---|
century(date) | Century. |
day(date) | Day. |
dayofmonth(date) | Day (synonym). |
dayofweek(date) | Numeric weekday (Sunday = 0, Saturday = 6). |
dayofyear(date) | Day of the year (starts from 1, i.e., January 1 = 1). |
decade(date) | Decade (year / 10). |
epoch(date) | Seconds since 1970-01-01. |
era(date) | Calendar era. |
hour(date) | Hours. |
isodow(date) | Numeric ISO weekday (Monday = 1, Sunday = 7). |
isoyear(date) | ISO Year number (Starts on Monday of week containing Jan 4th). |
julian(date) | DOUBLE Julian Day number. |
microsecond(date) | Sub-minute microseconds. |
millennium(date) | Millennium. |
millisecond(date) | Sub-minute milliseconds. |
minute(date) | Minutes. |
month(date) | Month. |
quarter(date) | Quarter. |
second(date) | Seconds. |
timezone_hour(date) | Time zone offset hour portion. |
timezone_minute(date) | Time zone offset minutes portion. |
timezone(date) | Time zone offset in seconds. |
week(date) | ISO Week. |
weekday(date) | Numeric weekday synonym (Sunday = 0, Saturday = 6). |
weekofyear(date) | ISO Week (synonym). |
year(date) | Year. |
yearweek(date) | BIGINT of combined ISO Year number and 2-digit version of ISO Week number. |
century(date)
Century.
SELECT century(DATE '1992-02-15') AS century; century--------- 20day(date)
Day.
SELECT day(DATE '1992-02-15') AS day; day----- 15dayofmonth(date)
Day (synonym).
SELECT dayofmonth(DATE '1992-02-15') AS dayofmonth; dayofmonth------------ 15dayofweek(date)
Numeric weekday (Sunday = 0, Saturday = 6).
SELECT dayofweek(DATE '1992-02-15') AS dayofweek; dayofweek----------- 6dayofyear(date)
Day of the year (starts from 1, i.e., January 1 = 1).
SELECT dayofyear(DATE '1992-02-15') AS dayofyear; dayofyear----------- 46decade(date)
Decade (year / 10).
SELECT decade(DATE '1992-02-15') AS decade; decade-------- 199epoch(date)
Seconds since 1970-01-01.
SELECT epoch(DATE '1992-02-15') AS epoch; epoch----------- 698112000era(date)
Calendar era.
SELECT era(DATE '0044-03-15 (BC)') AS era; era----- 0hour(date)
Hours.
SELECT hour(timestamp '2021-08-03 11:59:44.123456') AS hour; hour------ 11isodow(date)
Numeric ISO weekday (Monday = 1, Sunday = 7).
SELECT isodow(DATE '1992-02-15') AS isodow; isodow-------- 6isoyear(date)
ISO Year number (Starts on Monday of week containing Jan 4th).
SELECT isoyear(DATE '2022-01-01') AS isoyear; isoyear--------- 2021julian(date)
DOUBLE Julian Day number.
SELECT julian(DATE '1992-09-20') AS julian; julian--------- 2448886microsecond(date)
Sub-minute microseconds.
SELECT microsecond(timestamp '2021-08-03 11:59:44.123456') AS microsecond; microsecond------------- 44123456millennium(date)
Millennium.
SELECT millennium(DATE '1992-02-15') AS millennium; millennium------------ 2millisecond(date)
Sub-minute milliseconds.
SELECT millisecond(timestamp '2021-08-03 11:59:44.123456') AS millisecond; millisecond------------- 44123minute(date)
Minutes.
SELECT minute(timestamp '2021-08-03 11:59:44.123456') AS minute; minute-------- 59month(date)
Month.
SELECT month(DATE '1992-02-15') AS month; month------- 2quarter(date)
Quarter.
SELECT quarter(DATE '1992-02-15') AS quarter; quarter--------- 1second(date)
Seconds.
SELECT second(timestamp '2021-08-03 11:59:44.123456') AS second; second-------- 44timezone_hour(date)
Time zone offset hour portion.
SELECT timezone_hour(TIMESTAMP '2021-08-03 11:59:44.123456') AS timezone_hour; timezone_hour--------------- 0timezone_minute(date)
Time zone offset minutes portion.
SELECT timezone_minute(TIMESTAMP '2021-08-03 11:59:44.123456') AS timezone_minute; timezone_minute----------------- 0timezone(date)
Time zone offset in seconds.
SELECT timezone(TIMESTAMP '2021-08-03 11:59:44.123456') AS timezone; timezone---------- 0week(date)
ISO Week.
SELECT week(DATE '1992-02-15') AS week; week------ 7weekday(date)
Numeric weekday synonym (Sunday = 0, Saturday = 6).
SELECT weekday(DATE '1992-02-15') AS weekday; weekday--------- 6weekofyear(date)
ISO Week (synonym).
SELECT weekofyear(DATE '1992-02-15') AS weekofyear; weekofyear------------ 7year(date)
Year.
SELECT year(DATE '1992-02-15') AS year; year------ 1992yearweek(date)
BIGINT of combined ISO Year number and 2-digit version of ISO Week number.
SELECT yearweek(DATE '1992-02-15') AS yearweek; yearweek---------- 199207