Date and time part functions
Use these functions to read one part of a date, time, timestamp, or interval as a number, such as the year, the hour, or the second. You can also get the name of a date's month, or the number of days in its month.
century function
Returns the century that value falls in. Centuries are counted from year 1, so the 21st century is the years 2001 through 2100, and century returns 20 for a date in the year 2000. For a date before year 1 (BC), the result is negative. For an INTERVAL, the result is the number of whole centuries in its years and months. If value is NULL, the result is NULL.
century( returns the same result as value)date_part('century', .value)
Syntax
century(value)
Arguments
The century function takes the following argument:
value-
The value to read the century from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the century of a timestamp in 2021.
SELECT century( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the year 2000 is the last year of the 20th century.
SELECT century(DATE '2000-12-31') AS year_2000, century(DATE '2001-01-01') AS year_2001
The query returns the following result.
| year_2000 | year_2001 |
|---|---|
|
|
The following SQL query returns the number of whole centuries in an interval of 250 years.
SELECT century(INTERVAL 250 YEAR) AS result
The query returns the following result.
| result |
|---|
|
date_part function
Returns the part of value as a number. For example, 'year' returns the year, and 'hour' returns the hour of the day. date_part( returns the same result as part, value)extract(. If part FROM value)value is NULL, the result is NULL.
With a list of part names, date_part returns all of them at once, as a STRUCT that has one field for each part, in the order of the list.
For an INTERVAL, each part is read from the interval as it is stored, so date_part('hour', INTERVAL '90 minutes') returns 1 and date_part('minute', INTERVAL '90 minutes') returns 30. The datepart function takes the same arguments and returns the same results.
Syntax
date_part(part, value)
date_part(part_list, value)
Arguments
The date_part function takes the following arguments:
part-
The name of the part to read, as a
VARCHARvalue, such as'year','month','day','hour','dow'(the day of the week, with Sunday as 0), or'epoch'. Case is ignored, and plural and short forms such as'years'and'y'also work. For the full list of part names, see Date and time functions and operators in DuckDB. part_list-
The parts to read, as a list of
VARCHARpart names, such as['year', 'month', 'day']. value-
The value to read the part from, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue. Nanosecond values such asTIMESTAMP_NSare accepted too.
Return type
BIGINT for most parts, and DOUBLE for 'epoch' and 'julian'. With part_list, a STRUCT whose fields have those types.
Examples
The following SQL query returns the minute of a timestamp.
SELECT date_part( 'minute', TIMESTAMP '1992-09-20 20:38:40' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the year, month, and day of a date in one STRUCT.
SELECT date_part( ['year', 'month', 'day'], DATE '2026-11-18' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that 'millisecond' and 'microsecond' include the seconds.
SELECT date_part( 'second', TIMESTAMP '2026-11-18 10:11:12.345678' ) AS second_part, date_part( 'millisecond', TIMESTAMP '2026-11-18 10:11:12.345678' ) AS millisecond_part, date_part( 'microsecond', TIMESTAMP '2026-11-18 10:11:12.345678' ) AS microsecond_part
The query returns the following result.
| second_part | millisecond_part | microsecond_part |
|---|---|---|
|
|
|
The following SQL query reads the hour of a TIMESTAMP WITH TIME ZONE value. 10:00 at UTC+5 is 05:00 in UTC, so the result is 5.
SELECT date_part( 'hour', TIMESTAMPTZ '2026-11-18 10:00:00+05' ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
'second' returns whole seconds. 'millisecond' and 'microsecond' include the seconds, so for a time of 10:11:12.345678 they return 12345 and 12345678. To get only the milliseconds within the second, use date_part('millisecond', .value) % 1000
A TIMESTAMP WITH TIME ZONE value is read in UTC, not in the time zone that it was written with. For the same reason, 'timezone' returns 0 for a TIMESTAMP WITH TIME ZONE value. A TIME WITH TIME ZONE value keeps its offset, so 'timezone' returns that offset in seconds, such as -25200 for TIMETZ '18:18:16-07:00'.
day function
Returns the day of the month of value, from 1 to 31. For an INTERVAL, day returns only the days part. It does not convert months or hours to days, so day(INTERVAL 36 HOUR) returns 0. If value is NULL, the result is NULL.
day( returns the same result as value)date_part('day', .value)
dayofmonth is an alias of day.
Syntax
day(value)
Arguments
The day function takes the following argument:
value-
The value to read the day from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the day of the month of a timestamp.
SELECT day( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the days part of three intervals. The months and the hours are not converted to days.
SELECT day(INTERVAL '40 days') AS days_40, day(INTERVAL '1 month 5 days') AS month_and_5_days, day(INTERVAL 36 HOUR) AS hours_36
The query returns the following result.
| days_40 | month_and_5_days | hours_36 |
|---|---|---|
|
|
|
The following SQL query reads the day of a TIMESTAMP WITH TIME ZONE value. At UTC-5, 22:00 on November 18 is 03:00 on November 19 in UTC, so the result is 19.
SELECT day(TIMESTAMPTZ '2026-11-18 22:00:00-05') AS result
The query returns the following result.
| result |
|---|
|
Considerations
A TIMESTAMP WITH TIME ZONE value is read in UTC, so the day can differ from the day in the time zone that the value was written with.
Differences from Athena engine version 3
In Athena engine version 3, a TIMESTAMP WITH TIME ZONE value keeps its time zone, and day returns the day in that time zone. In DuckDB, the value is read in UTC. For example, for 22:00 on November 18 at UTC-5, Athena engine version 3 returns 18, and DuckDB returns 19.
dayofyear function
Returns the day of the year of ts, where January 1 is 1. December 31 is 365, or 366 in a leap year. If ts is NULL, the result is NULL.
Syntax
dayofyear(ts)
Arguments
The dayofyear function takes the following argument:
ts-
The value to read the day of the year from, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
BIGINT
Examples
The following SQL query returns the day of the year of a timestamp.
SELECT dayofyear( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the day of the year of December 31 in a leap year and in a common year.
SELECT dayofyear(DATE '2024-12-31') AS leap_year, dayofyear(DATE '2023-12-31') AS common_year
The query returns the following result.
| leap_year | common_year |
|---|---|
|
|
Considerations
For a TIMESTAMP WITH TIME ZONE value, the day is taken in the session time zone, which is UTC.
dayofyear does not work on INTERVAL values. A call such as dayofyear(INTERVAL '3 days') fails.
Differences from Athena engine version 3
Athena engine version 3 names this function day_of_year. DuckDB does not have day_of_year, so use dayofyear.
days_in_month function
Returns the number of days in the month that contains date, a number from 28 to 31. February has 29 days in a leap year. If date is NULL, the result is NULL.
days_in_month is a macro that returns day(last_day(. To get the last day of the month as a date, use the last_day function.date))
Syntax
days_in_month(date)
Arguments
The days_in_month function takes the following argument:
date-
A value in the month whose days to count, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
BIGINT
Examples
The following SQL query returns the number of days in February 2024, a leap year.
SELECT days_in_month(DATE '2024-02-10') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the number of days in February 2023, a common year.
SELECT days_in_month(DATE '2023-02-10') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts the string '2023-04-10' to DATE first, and returns the number of days in April 2023.
SELECT days_in_month(CAST('2023-04-10' AS DATE)) AS result
The query returns the following result.
| result |
|---|
|
Considerations
A string literal is not converted to a date for you. A call such as days_in_month('2023-04-10') fails, so cast the string first, as in days_in_month(CAST('2023-04-10' AS DATE)).
decade function
Returns the year of ts divided by 10, with the remainder dropped. For example, every date from 2020 through 2029 returns 202. For an INTERVAL value, returns the number of whole years in the interval divided by 10. If ts is NULL, the result is NULL.
To get the century or the millennium, use the century function or the millennium function.
Syntax
decade(ts)
Arguments
The decade function takes the following argument:
ts-
The value to read the decade from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the decade of a timestamp in 2021.
SELECT decade( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a decade runs from a year that ends in 0 through a year that ends in 9.
SELECT decade(DATE '1999-12-31') AS last_day_of_1990s, decade(DATE '2000-01-01') AS first_day_of_2000s
The query returns the following result.
| last_day_of_1990s | first_day_of_2000s |
|---|---|
|
|
The following SQL query returns the number of whole decades in an interval of 25 years.
SELECT decade(INTERVAL '25 years') AS result
The query returns the following result.
| result |
|---|
|
era function
Returns 1 when ts is in year 1 or later (AD), and 0 when it is before year 1 (BC). If ts is NULL, the result is NULL.
To write a date before year 1 as a literal, add (BC) after the date, as in DATE '0044-03-15 (BC)'.
Syntax
era(ts)
Arguments
The era function takes the following argument:
ts-
The value to read the era from, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
BIGINT
Examples
The following SQL query returns the era of a timestamp in 2021.
SELECT era( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares a date in 44 BC with the same day and month in AD 44.
SELECT era(DATE '0044-03-15 (BC)') AS bc_date, era(DATE '0044-03-15') AS ad_date
The query returns the following result.
| bc_date | ad_date |
|---|---|
|
|
Considerations
era does not work on INTERVAL values. A call such as era(INTERVAL '5 years') fails.
hour function
Returns the hour of ts, a number from 0 to 23. For a DATE value, the result is 0. If ts is NULL, the result is NULL.
For an INTERVAL value, returns the hours part of the interval. An interval stores days separately from hours, so the result can be 24 or more: hour(INTERVAL '30 hours') returns 30, but hour(INTERVAL '1 day 3 hours') returns 3.
Syntax
hour(ts)
Arguments
The hour function takes the following argument:
ts-
The value to read the hour from, as a
DATE,TIME,TIME_NS,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP_NS,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the hour of a timestamp.
SELECT hour( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes 11:15 PM at UTC offset +05. In UTC, that time is 6:15 PM, so the function returns 18.
SELECT hour(TIMESTAMPTZ '2021-08-03 23:15:00+05') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the hours part of two intervals. The second interval stores its day separately from its hours, so the function returns only its 3 hours.
SELECT hour(INTERVAL '30 hours') AS thirty_hours, hour( INTERVAL '1 day 3 hours' ) AS one_day_three_hours
The query returns the following result.
| thirty_hours | one_day_three_hours |
|---|---|
|
|
Considerations
For a TIMESTAMP WITH TIME ZONE value, the hour is taken in the session time zone, which is UTC, not at the offset that the value was written with.
For a TIME WITH TIME ZONE value, the hour is the one written in the value, and the offset is not applied.
microsecond function
Returns the seconds part of ts, including its fraction, as a number of microseconds. The result is from 0 to 59999999, so 11:59:44.123456 gives 44123456, not 123456. For a DATE, the result is 0. For an INTERVAL, the result is the seconds part of its time after whole minutes are removed, so INTERVAL '90 seconds' gives 30000000.
microsecond( returns the same value as ts)date_part('microseconds', . If ts)ts is NULL, the result is NULL.
Syntax
microsecond(ts)
Arguments
The microsecond function takes the following argument:
ts-
The value to read the seconds and fractional seconds from, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE,TIMESTAMP_NS, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the seconds of a timestamp in microseconds.
SELECT microsecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares the full result with the fractional part only.
SELECT microsecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS total_us, microsecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) % 1000000 AS fraction_us
The query returns the following result.
| total_us | fraction_us |
|---|---|
|
|
Considerations
To get only the fractional part of the second, from 0 to 999999, use microsecond(.ts) % 1000000
millennium function
Returns the millennium that ts falls in. Millennia start with year 1, so years 1 to 1000 are millennium 1 and years 2001 to 3000 are millennium 3. The year 2000 is in millennium 2. For an INTERVAL, the result is the number of whole millennia in its years part.
millennium( returns the same value as ts)date_part('millennium', . If ts)ts is NULL, the result is NULL.
Syntax
millennium(ts)
Arguments
The millennium function takes the following argument:
ts-
The value to read the millennium from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the millennium of a timestamp.
SELECT millennium( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the last day of 2000 is still in the second millennium, and the third millennium starts on January 1, 2001.
SELECT millennium(DATE '2000-12-31') AS last_day_2000, millennium(DATE '2001-01-01') AS first_day_2001
The query returns the following result.
| last_day_2000 | first_day_2001 |
|---|---|
|
|
The following SQL query returns the number of whole millennia in an interval of 2,500 years.
SELECT millennium(INTERVAL '2500 years') AS result
The query returns the following result.
| result |
|---|
|
millisecond function
Returns the seconds part of ts, including its fraction, as a whole number of milliseconds. The result is from 0 to 59999, so 11:59:44.123456 gives 44123, not 123. Digits finer than a millisecond are dropped. For a DATE, the result is 0.
millisecond( returns the same value as ts)date_part('millisecond', . If ts)ts is NULL, the result is NULL.
Syntax
millisecond(ts)
Arguments
The millisecond function takes the following argument:
ts-
The value to read the seconds and fractional seconds from, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE,TIMESTAMP_NS, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the seconds of a timestamp in milliseconds.
SELECT millisecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares the full result with the milliseconds within the second only.
SELECT millisecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS total_ms, millisecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) % 1000 AS within_second_ms
The query returns the following result.
| total_ms | within_second_ms |
|---|---|
|
|
Considerations
To get only the milliseconds within the second, from 0 to 999, use millisecond(.ts) % 1000
Differences from Athena engine version 3
In Athena engine version 3, millisecond returns only the milliseconds within the second, from 0 to 999. In DuckDB, the result includes the seconds, from 0 to 59999. To get the Athena engine version 3 result, use millisecond(.ts) % 1000
minute function
Returns the minute of ts, from 0 to 59. For a DATE, the result is 0. For an INTERVAL, the result is the minutes part of its time, not its total length in minutes, so INTERVAL '150 minutes' gives 30.
A TIMESTAMP WITH TIME ZONE value is read in the session time zone, which is UTC. minute( returns the same value as ts)date_part('minute', . If ts)ts is NULL, the result is NULL.
Syntax
minute(ts)
Arguments
The minute function takes the following argument:
ts-
The value to read the minute from, as a
DATE,TIME,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE,TIMESTAMP_NS, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the minute of a timestamp.
SELECT minute( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the minutes part of an interval of 150 minutes, which is 2 hours and 30 minutes.
SELECT minute(INTERVAL '150 minutes') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reads the minute of a timestamp with time zone in UTC, and then in the time zone of the original value.
SELECT minute( TIMESTAMPTZ '2021-08-03 11:59:44+05:30' ) AS utc_minute, minute( TIMESTAMPTZ '2021-08-03 11:59:44+05:30' AT TIME ZONE 'Asia/Kolkata' ) AS local_minute
The query returns the following result.
| utc_minute | local_minute |
|---|---|
|
|
Considerations
Some time zones have an offset that is not a whole number of hours, such as India Standard Time (UTC+5:30). For a TIMESTAMP WITH TIME ZONE value in such a time zone, the minute in UTC differs from the local minute. To read the local minute, convert the value first, as in minute(.ts AT TIME ZONE 'Asia/Kolkata')
Differences from Athena engine version 3
In Athena engine version 3, a TIMESTAMP WITH TIME ZONE value keeps its own time zone, and minute returns the minute in that zone. In DuckDB, the value is read in the session time zone, UTC. For 11:59:44 at UTC+5:30, Athena engine version 3 returns 59 and DuckDB returns 29. To get the Athena engine version 3 result, convert the value with AT TIME ZONE before you call minute.
month function
Returns the month of ts, from 1 for January to 12 for December. For an INTERVAL, the result is the months that are left after whole years. This value is from 0 to 11, or from -11 to 0 for a negative interval. For example, INTERVAL '-14 months' gives -2. Days in an interval are not converted to months, so INTERVAL '45 days' gives 0.
A TIMESTAMP WITH TIME ZONE value is read in the session time zone, which is UTC. month( returns the same value as ts)date_part('month', and ts)EXTRACT(MONTH FROM . If ts)ts is NULL, the result is NULL.
Syntax
month(ts)
Arguments
The month function takes the following argument:
ts-
The value to read the month from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the month of a timestamp.
SELECT month( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the months part of an interval of 14 months, and of an interval of 45 days, whose days are not counted as months.
SELECT month(INTERVAL '14 months') AS from_months, month(INTERVAL '45 days') AS from_days
The query returns the following result.
| from_months | from_days |
|---|---|
|
|
The following SQL query reads the month of a timestamp with time zone in UTC, and then in a time zone that is 5 hours behind UTC on that date.
SELECT month( TIMESTAMPTZ '2021-08-31 23:30:00-05' ) AS utc_month, month( TIMESTAMPTZ '2021-08-31 23:30:00-05' AT TIME ZONE 'America/Chicago' ) AS local_month
The query returns the following result.
| utc_month | local_month |
|---|---|
|
|
Considerations
month does not accept a string. A call such as month('2021-08-03') fails, so cast the string first, as in month(CAST('2021-08-03' AS DATE)).
For a TIMESTAMP WITH TIME ZONE value near the end of a month, the month in UTC can differ from the local month. To read the local month, convert the value first, as in month(.ts AT TIME ZONE 'America/Chicago')
Differences from Athena engine version 3
In Athena engine version 3, a TIMESTAMP WITH TIME ZONE value keeps its own time zone, and month returns the month in that zone. In DuckDB, the value is read in the session time zone, UTC. For 2021-08-31 23:30 at UTC-5, Athena engine version 3 returns 8 and DuckDB returns 9. To get the Athena engine version 3 result, convert the value with AT TIME ZONE before you call month.
monthname function
Returns the month of ts spelled out in full as an English word, such as September. For a three-letter abbreviation, such as Sep, use strftime(.ts, '%b')
A TIMESTAMP WITH TIME ZONE value is read in the session time zone, which is UTC. If ts is NULL, the result is NULL.
Syntax
monthname(ts)
Arguments
The monthname function takes the following argument:
ts-
The value to read the month from, as a
DATE,TIMESTAMP, orTIMESTAMP WITH TIME ZONEvalue.
Return type
VARCHAR
Examples
The following SQL query returns the name of the month of a timestamp.
SELECT monthname(TIMESTAMP '1992-09-20') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query reads a timestamp with time zone in UTC, and then in a time zone that is 5 hours behind UTC on that date. The two months differ.
SELECT monthname( TIMESTAMPTZ '2021-08-31 23:30:00-05' ) AS utc_month, monthname( TIMESTAMPTZ '2021-08-31 23:30:00-05' AT TIME ZONE 'America/Chicago' ) AS local_month
The query returns the following result.
| utc_month | local_month |
|---|---|
|
|
Considerations
monthname does not accept a string. Cast a string to DATE or TIMESTAMP first.
Differences from Athena engine version 3
In Athena engine version 3, you get the English month name with date_format(. DuckDB has no ts, '%M')date_format function, and in strftime, %M is the minute, not the month name. Replace the call with monthname( or ts)strftime(.ts, '%B')
nanosecond function
Returns the seconds part of ts, including its fraction, as a number of nanoseconds. The result is from 0 to 59999999999, so 11:59:44.123456789 gives 44123456789. For a DATE, the result is 0.
Only TIMESTAMP_NS, TIMESTAMPTZ_NS, and TIME_NS values hold nanoseconds. Other types hold at most microseconds, so for them the last three digits of the result are always 0. If ts is NULL, the result is NULL.
Syntax
nanosecond(ts)
Arguments
The nanosecond function takes the following argument:
ts-
The value to read the seconds and fractional seconds from, as a
DATE,TIME,TIME_NS,TIME WITH TIME ZONE,TIMESTAMP,TIMESTAMP WITH TIME ZONE,TIMESTAMP_NS,TIMESTAMPTZ_NS, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the seconds of a TIMESTAMP_NS value in nanoseconds.
SELECT nanosecond( TIMESTAMP_NS '2021-08-03 11:59:44.123456789' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes a TIMESTAMP value, which holds microseconds, so the last three digits of the result are 0.
SELECT nanosecond( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns only the fractional part of the second, in nanoseconds.
SELECT nanosecond( TIMESTAMP_NS '2021-08-03 11:59:44.123456789' ) % 1000000000 AS result
The query returns the following result.
| result |
|---|
|
Considerations
To get only the fractional part of the second, from 0 to 999999999, use nanosecond(.ts) % 1000000000
quarter function
Returns the calendar quarter that value falls in, as a number from 1 to 4. January through March is quarter 1, and October through December is quarter 4. If value is NULL, the result is NULL.
For an INTERVAL, the function uses only the months that remain after whole years are removed, and ignores days and smaller units. For example, quarter(INTERVAL '7 months') returns 3, and quarter(INTERVAL '14 months') returns 1. A negative interval can give a result outside the range from 1 to 4, such as 0 for INTERVAL '-3 months' and -1 for INTERVAL '-7 months'.
quarter( returns the same result as value)extract(quarter FROM and value)date_part('quarter', .value)
Syntax
quarter(value)
Arguments
The quarter function takes the following argument:
value-
The value to read the quarter from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the quarter of a timestamp in August.
SELECT quarter( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the quarter of dates at the start and end of quarters.
SELECT d AS order_date, quarter(d) AS order_quarter FROM (VALUES (DATE '2024-01-01'), (DATE '2024-03-31'), (DATE '2024-04-01'), (DATE '2024-12-31') ) AS v(d) ORDER BY d
The query returns the following results.
| order_date | order_quarter |
|---|---|
|
|
|
|
|
|
|
|
The following SQL query passes 23:30 on March 31 at a UTC offset of -05. In UTC, that is 04:30 on April 1, so the function returns quarter 2.
SELECT quarter( TIMESTAMPTZ '2024-03-31 23:30:00-05' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the quarter for two intervals. The 14-month interval counts as 2 months after the whole year is removed.
SELECT quarter(INTERVAL '7 months') AS seven_months, quarter(INTERVAL '14 months') AS fourteen_months
The query returns the following result.
| seven_months | fourteen_months |
|---|---|
|
|
Considerations
Pass a typed value. A string, such as quarter('2024-05-05'), makes the query fail. Write a typed literal such as DATE '2024-05-05', or cast the string to DATE or TIMESTAMP first.
For a TIMESTAMP WITH TIME ZONE value, the function computes the quarter in the session time zone, which is UTC. A time late on the last day of a quarter in a zone west of UTC can belong to the next quarter.
second function
Returns the seconds part of value as a whole number. For a time or timestamp, the result is from 0 to 59, and any fraction of a second is dropped, not rounded. For a DATE, the result is 0. If value is NULL, the result is NULL.
For an INTERVAL, the function returns the seconds that remain after whole minutes are removed, so second(INTERVAL '90 seconds') returns 30. A negative interval gives a negative result.
second( returns the same result as value)extract(second FROM . To keep the fraction, use the millisecond function or the microsecond function, whose results include the whole seconds.value)
Syntax
second(value)
Arguments
The second function takes the following argument:
value-
The value to read the seconds from, as a
TIME,TIME WITH TIME ZONE,DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue.
Return type
BIGINT
Examples
The following SQL query returns the seconds part of a timestamp.
SELECT second( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the fraction is dropped, not rounded. millisecond returns the seconds and the fraction together, in milliseconds.
SELECT second( TIMESTAMP '2021-08-03 11:59:44.987654' ) AS seconds, millisecond( TIMESTAMP '2021-08-03 11:59:44.987654' ) AS milliseconds
The query returns the following result.
| seconds | milliseconds |
|---|---|
|
|
The following SQL query returns the seconds part of a 90-second interval, which is 1 minute and 30 seconds.
SELECT second(INTERVAL '90 seconds') AS result
The query returns the following result.
| result |
|---|
|
year function
Returns the calendar year of ts. If ts is NULL, the result is NULL.
For an INTERVAL, year returns the number of whole years in its months part, so INTERVAL 30 MONTH gives 2. Days and smaller units are not converted to years, so INTERVAL 400 DAY gives 0.
year returns the calendar year. To get the year that an ISO 8601 week belongs to, use the isoyear function.
Syntax
year(ts)
Arguments
The year function takes the following argument:
ts-
The value to read the year from, as a
DATE,TIMESTAMP,TIMESTAMP WITH TIME ZONE, orINTERVALvalue. Nanosecond values,TIMESTAMP_NSandTIMESTAMPTZ_NS, are accepted too.
Return type
BIGINT
Examples
The following SQL query returns the year of a timestamp.
SELECT year( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares the calendar year with the ISO year for January 1, 2021, which is in the last ISO week of 2020.
SELECT year(DATE '2021-01-01') AS calendar_year, isoyear(DATE '2021-01-01') AS iso_year
The query returns the following result.
| calendar_year | iso_year |
|---|---|
|
|
The following SQL query returns the number of whole years in an interval of 30 months.
SELECT year(INTERVAL 30 MONTH) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes 11:30 PM on December 31, 2021, at UTC offset -05. In UTC, that time is in 2022.
SELECT year(TIMESTAMPTZ '2021-12-31 23:30:00-05') AS result
The query returns the following result.
| result |
|---|
|
Considerations
For a TIMESTAMP WITH TIME ZONE value, the year is taken in the session time zone, which is UTC. A time late on December 31 in a time zone west of UTC can already be in the next year.
Pass a date or timestamp, not a string. A call such as year('2021-08-03') fails. Write year(DATE '2021-08-03'), or cast the string to DATE.