+ similarly to
+ linkend="function-justify-hours">justify_hours() )
timestamp '2001-09-29 03:00' - timestamp '2001-07-27 12:00'
|
-
+ id="function-justify-days">
justify_days ( interval )
interval
- Adjust interval so 30-day time periods are represented as months
+ Adjust interval, converting 30-day time periods to months
- justify_days(interval '3 5 days')
- 1 mon 5 days
+ justify_days(interval '1 year 6 5 days')
+ 1 year 2 mons 5 days
|
-
+ id="function-justify-hours">
justify_hours ( interval )
interval
- Adjust interval so 24-hour time periods are represented as days
+ Adjust interval, converting 24-hour time periods to days
- justify_hours(interval '27 hour s')
- 1 day 03:0 0:00
+ justify_hours(interval '50 hours 10 minute s')
+ 2 days 02:1 0:00
The extract function retrieves subfields
such as year or hour from date/time values.
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
+ type timestamp , date , time ,
+ or interval . (Timestamps and times can be with or
+ without time zone.)
+ field is an identifier or
string that selects what field to extract from the source value.
+ Not all fields are valid for every input data type; for example, fields
+ smaller than a day cannot be extracted from a date , while
+ fields of a day or more cannot be extracted from a time .
The extract function returns values of type
numeric .
+
+
The following are valid field names:
century
- The century
+ The century; for interval values, the year field
+ divided by 100
Result: 20
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 21
+SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD');
+Result: 1
+SELECT EXTRACT(CENTURY FROM DATE '0001-12-31 BC');
+Result: -1
+SELECT EXTRACT(CENTURY FROM INTERVAL '2001 years');
+Result: 20
-
- The first century starts at 0001-01-01 00:00:00 AD, although
- they did not know it at the time. This definition applies to all
- Gregorian calendar countries. There is no century number 0,
- you go from -1 century to 1 century.
-
- If you disagree with this, please write your complaint to:
- Pope, Cathedral Saint-Peter of Roma, Vatican.
-
day
- For timestamp values, the day (of the month) field
- (1–31) ; for interval values, the number of days
+ The day of the month (1–31); for interval
+ values, the number of days
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
-
SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
Result: 40
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
Result: 982384720.120000
-
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40.12');
Result: 982355920.120000
-
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
Result: 442800.000000
hour
- The hour field (0–23)
+ The hour field (0–23 in timestamps, unrestricted in
+ intervals)
The
ISO 8601 week-numbering year that the date
- falls in (not applicable to intervals)
+ falls in
different from the Gregorian year. See the week
field for more information.
- This field is not available in PostgreSQL releases prior to 8.3.
-
The Julian Date corresponding to the
- date or timestamp (not applicable to intervals) . Timestamps
+ date or timestamp. Timestamps
that are not local midnight result in a fractional value. See
for more information.
millennium
- The millennium
+ The millennium; for interval values, the year field
+ divided by 1000
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 3
+SELECT EXTRACT(MILLENNIUM FROM INTERVAL '2001 years');
+Result: 2
month
- For timestamp values, the number of the month
- within the year (1–12) ; for interval values,
- the number of months, modulo 12 (0–11)
+ The number of the month within the year (1–12);
+ for interval values, the number of months modulo 12
+ (0–11)
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 2
-
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
Result: 3
-
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
Result: 1
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 40.000000
-
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
Result: 28.500000
+ When processing an interval value,
+ the extract function produces field values that
+ match the interpretation used by the interval output function. This
+ can produce surprising results if one starts with a non-normalized
+ interval representation, for example:
+
+SELECT INTERVAL '80 minutes';
+Result: 01:20:00
+SELECT EXTRACT(MINUTES FROM INTERVAL '80 minutes');
+Result: 20
+
+
+
When the input value is +/-Infinity, extract returns
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
Result: 16
-
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
Result: 4
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00
-
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00
-
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
Result: 2001-02-16 00:00:00-05
-
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
Result: 2001-02-16 08:00:00-05
-
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
Result: 3 days 02:00:00
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01');
Result: 2020-02-11 15:30:00
-
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
Result: 2020-02-11 15:32:30
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'America/Denver';
Result: 2001-02-16 19:38:40-08
-
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'America/Denver';
Result: 2001-02-16 18:38:40
-
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'Asia/Tokyo' AT TIME ZONE 'America/Chicago';
Result: 2001-02-16 05:38:40
SELECT CURRENT_TIME;
Result: 14:39:53.662522-05
-
SELECT CURRENT_DATE;
Result: 2019-12-23
-
SELECT CURRENT_TIMESTAMP;
Result: 2019-12-23 14:39:53.662522-05
-
SELECT CURRENT_TIMESTAMP(2);
Result: 2019-12-23 14:39:53.66-05
-
SELECT LOCALTIMESTAMP;
Result: 2019-12-23 14:39:53.662522