Time and Date Extraction Functions
date_part(text, timestamp | interval)
Description: Extracts a specified part (text) from a date or time expression (timestamp or interval). It is equivalent to the extract(field from timestamp) function.
Syntax:
date_part('field', source)
- source must be a value expression of type timestamp, time, or interval. (Expressions of type date are cast to timestamp and can therefore be used as well.)
- field is an identifier or a string that specifies the time part to be extracted from the source data. For the value of field, see Table 1.
Return type: double precision
Examples:
Obtain the hour of a specified date.
1 2 3 4 5 |
SELECT date_part('hour', timestamp '2001-02-16 20:38:40'); date_part ----------- 20 (1 row) |
Extract the month from the time interval 2 years 3 months.
1 2 3 4 5 |
SELECT date_part('month', interval '2 years 3 months'); date_part ----------- 3 (1 row) |
date_trunc(text, timestamp with time zone | timestamp without time zone | interval)
Description: Truncates a time or date to the specified precision (text).
text cam be microseconds, milliseconds, second, minute, hour, day, week, month, quarter, year, decade, century, and millennium.
Return type: timestamp with time zone, timestamp without time zone, and interval
Examples:
Truncate the input time to the hour.
1 2 3 4 5 |
SELECT date_trunc('hour', timestamp '2025-02-16 20:38:40'); date_trunc --------------------- 2025-02-16 20:00:00 (1 row) |
Truncate the input time to the day.
1 2 3 4 5 |
SELECT date_trunc('day', timestamptz'2025-02-16 20:38:40+08'); date_trunc ------------------------ 2025-02-16 00:00:00-05 (1 row) |
Truncate the input time to the month.
1 2 3 4 5 |
SELECT date_trunc('month', timestamp '2025-02-16 20:38:40'); date_trunc --------------------- 2025-02-01 00:00:00 (1 row) |
Truncate the input timestamp to the first day of a quarter.
1 2 3 4 5 |
SELECT DATE_TRUNC('quarter', TIMESTAMP '20250430 04:05:06.789'); date_trunc --------------------- 2025-04-01 00:00:00 (1 row) |
Truncate the first day of the current month at 12:00.
1 2 3 4 5 |
SELECT date_trunc('month',now()) +interval '12h'; ?column? ------------------------ 2025-09-01 12:00:00-04 (1 row) |
Truncate 09:00 of the current day.
1 2 3 4 5 |
SELECT date_trunc('day',now()) + interval '9h'; ?column? ------------------------ 2025-09-22 09:00:00-04 (1 row) |
Truncate the first day of the current year.
1 2 3 4 5 |
SELECT date_trunc('year',CURRENT_DATE); date_trunc ------------------------ 2025-01-01 00:00:00-05 (1 row) |
trunc(timestamp)
Description: Truncates a timestamp to day.
Return type: timestamp
Example:
1 2 3 4 |
SELECT trunc(timestamp '2001-02-16 20:38:40'); trunc --------------------- 2001-02-16 00:00:00 (1 row) |
extract(field from timestamp | time | interval)
Description: Extracts a specified time part from a date or time expression, for example, the year, month, and hour.
Syntax:
EXTRACT(field FROM source)
- source must be a value expression of type timestamp, time, or interval. (Expressions of type date are cast to timestamp and can therefore be used as well.)
- field is an identifier or a string that specifies the time part to be extracted from the source data. For the value of field, see Table 1.
Return type: double precision
|
Field |
Example |
Return Result |
||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
century |
Extract the century from the given timestamp.
The first century starts at 0001-01-01 00:00:00 AD. This definition applies to all Gregorian calendar countries. There is no 0th century. The timeline transitions directly from the 1st century BC to the 1st century AD. |
Return result:
|
||||||||||
|
millennium |
Extract the millennium from the specified timestamp.
Years in the 1900s are in the second millennium. The third millennium started from January 1, 2001. |
Return result:
|
||||||||||
|
decade |
Extract the decade from the specified timestamp.
|
Return result:
|
||||||||||
|
year |
Extract the year from the specified timestamp.
|
Return result:
|
||||||||||
|
quarter |
Extract the quarter from the specified timestamp. The value ranges from 1 to 4.
|
Return result:
|
||||||||||
|
month |
|
|
||||||||||
|
week |
ISO 8601 standard. The first Thursday of a year is in the first week. Each week starts from Monday and ends on Sunday. The value ranges from 1 to 53. Obtain the week number of the given date in a year.
|
Return result:
|
||||||||||
|
day |
|
|
||||||||||
|
hour |
Extract the hour from the specified timestamp. The value ranges from 0 to 23.
|
Return result:
|
||||||||||
|
minute |
Extract the minute from the specified timestamp.
|
Return result:
|
||||||||||
|
second |
Extract the second (including any fractional seconds) from the specified timestamp.
|
Return result:
|
||||||||||
|
milliseconds |
Extract the millisecond from the specified timestamp. Full seconds are returned in milliseconds, that is, the second part (including fractional parts) multiplied by 1000.
|
Return result:
|
||||||||||
|
microseconds |
Extract the microsecond from the specified timestamp. Full seconds are returned in microsecond, that is, the second part (including fractional parts) multiplied by 1,000,000.
|
Return result:
|
||||||||||
|
epoch |
Convert an epoch value back to a timestamp:
|
Return result:
Return result:
|
||||||||||
|
doy |
Obtain the day of a year in a specified timestamp. The value ranges from 1 to 365/366.
|
Return result:
|
||||||||||
|
isodow |
Obtain the day of a week in a specified timestamp. Monday is 1, and Sunday is 7. The value ranges from 1 to 7.
|
Return result:
|
||||||||||
|
dow |
Obtain the day of a week in a specified timestamp. Sunday is 0, and Saturday is 6. The value ranges from 0 to 6.
|
Return result:
|
||||||||||
|
isoyear |
Obtain the ISO 8601 week-numbering year of a specified date (not applicable to interval). Each ISO year begins with the Monday of the week containing January 4, so in early January or late December the ISO year may be different from the Gregorian year. For details, see the week field.
|
Return result:
Return result:
|
||||||||||
|
timezone |
The time zone offset from UTC, measured in seconds. Positive values correspond to time zones east of UTC, negative values to zones west of UTC.
|
Return result:
|
||||||||||
|
timezone_hour |
Extract the hour component of the time zone offset from a time value with a time zone.
|
Return result:
|
||||||||||
|
timezone_minute |
Extract the minute component of the time zone offset from a time value with a time zone.
|
Return result:
|
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot