Movatterモバイル変換


[0]ホーム

URL:


September 25, 2025: PostgreSQL 18 Released!
Supported Versions:Current (18) /17 /16 /15 /14 /13
Development Versions:devel
Unsupported versions:12 /11 /10 /9.6 /9.5 /9.4 /9.3 /9.2 /9.1 /9.0 /8.4 /8.3 /8.2 /8.1 /8.0 /7.4 /7.3 /7.2 /7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for thecurrent version, or one of the other supported versions listed above instead.
PostgreSQL 7.1.3 Documentation
PrevChapter 4. Functions and OperatorsNext

4.7. Date/Time Functions

Table 4-14 shows the available functions for date/time value processing. The basic arithmetic operators (+,*, etc.) are also available. For formatting functions, refer toSection 4.6. You should be familiar with the background information on date/time data types (seeSection 3.4).

Table 4-14. Date/Time Functions

NameReturn TypeDescriptionExampleResult
age(timestamp)intervalSubtract from todayage(timestamp '1957-06-13')43 years 8 mons 3 days
age(timestamp, timestamp)intervalSubtract argumentsage('2001-04-10', timestamp '1957-06-13')43 years 9 mons 27 days
current_datedateToday's date; seebelow  
current_timetimeTime of day; seebelow  
current_timestamptimestampdate and time; see alsobelow  
date_part(text, timestamp)double precisionGet subfield (equivalent toextract); see alsobelowdate_part('hour', timestamp '2001-02-16 20:38:40')20
date_part(text, interval)double precisionGet subfield (equivalent toextract); see alsobelowdate_part('month', interval '2 years 3 months')3
date_trunc(text, timestamp)timestampTruncate to specified precision; see alsobelowdate_trunc('hour', timestamp '2001-02-16 20:38:40')2001-02-16 20:00:00+00
extract(field from timestamp)double precisionGet subfield; see alsobelowextract(hour from timestamp '2001-02-16 20:38:40')20
extract(field from interval)double precisionGet subfield; see alsobelowextract(month from interval '2 years 3 months')3
isfinite(timestamp)booleanTest for finite time stamp (neither invalid nor infinity)isfinite(timestamp '2001-02-16 21:28:30')true
isfinite(interval)booleanTest for finite intervalisfinite(interval '4 hours')true
now()timestampCurrent date and time (equivalent tocurrent_timestamp); see alsobelow  
timeofday()textHigh-precision date and time; see alsobelowtimeofday()Wed Feb 21 17:01:13.000126 2001 EST
timestamp(date)timestampDate to timestamptimestamp(date '2000-12-25')2000-12-25 00:00:00
timestamp(date, time)timestampDate and time to a timestamptimestamp(date '1998-02-24',time '23:07')1998-02-24 23:07:00

4.7.1.EXTRACT,date_part

EXTRACT (field FROMsource)

Theextract function retrieves sub-fields from date/time values, such as year or hour.source is a value expression that evaluates to typetimestamp orinterval. (Expressions of typedate ortime will be cast totimestamp and can therefore be used as well.)field is an identifier or string that selects what field to extract from the source value. Theextract function returns values of typedouble precision. The following are valid values:

century

The year field divided by 100

SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');Result:20

Note that the result for the century field is simply the year field divided by 100, and not the conventional definition which puts most years in the 1900's in the twentieth century.

day

The day (of the month) field (1 - 31)

SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');Result:16
decade

The year field divided by 10

SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');Result:200
dow

The day of the week (0 - 6; Sunday is 0) (fortimestamp values only)

SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');Result:5
doy

The day of the year (1 - 365/366) (fortimestamp values only)

SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');Result:47
epoch

Fordate andtimestamp values, the number of seconds since 1970-01-01 00:00:00 (Result may be negative.); forinterval values, the total number of seconds in the interval

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40');Result:982352320SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');Result:442800
hour

The hour field (0 - 23)

SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');Result:20
microseconds

The seconds field, including fractional parts, multiplied by 1 000 000. Note that this includes full seconds.

SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');Result:28500000
millennium

The year field divided by 1000

SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');Result:2

Note that the result for the millennium field is simply the year field divided by 1000, and not the conventional definition which puts years in the 1900's in the second millennium.

milliseconds

The seconds field, including fractional parts, multiplied by 1000. Note that this includes full seconds.

SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');Result:28500
minute

The minutes field (0 - 59)

SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');Result:38
month

Fortimestamp values, the number of the month within the year (1 - 12) ; forinterval values the number of months, modulo 12 (0 - 11)

SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');Result:2SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');Result:3SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');Result:1
quarter

The quarter of the year (1 - 4) that the day is in (fortimestamp values only)

SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');Result:1
second

The seconds field, including fractional parts (0 - 59[1])

SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');Result:40SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');Result:28.5
week

From atimestamp value, calculate the number of the week of the year that the day is in. By definition (ISO 8601), the first week of a year contains January 4 of that year. (TheISO week starts on Monday.) In other words, the first Thursday of a year is in week 1 of that year.

SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');Result:7
year

The year field

SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');Result:2001

Theextract function is primarily intended for computational processing. For formatting date/time values for display, seeSection 4.6.

Thedate_part function is modeled on the traditionalIngres equivalent to theSQL-functionextract:

date_part('field',source)
Note that here thefieldvalue needs to be a string. The valid field values fordate_part are the same as forextract.
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');Result:16SELECT date_part('hour', INTERVAL '4 hours 3 minutes')Result:4

4.7.2.date_trunc

The functiondate_trunc is conceptually similar to thetrunc function for numbers.

date_trunc('field',source)
source is a value expressionof typetimestamp (values of typedate andtime are cast automatically).field selects to which precision to truncate the time stamp value. The return value is of typetimestamp with all fields that are less than the selected one set to zero (or one, for day and month).

Valid values forfield are:

microseconds
milliseconds
second
minute
hour
day
month
year
decade
century
millennium
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');Result:2001-02-16 20:00:00+00SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');Result:2001-01-01 00:00:00+00

4.7.3. Current Date/Time

The following functions are available to obtain the current date and/or time:

CURRENT_TIMECURRENT_DATECURRENT_TIMESTAMP
Note that because of the requirements of theSQL standard, these functions must not be calledwith trailing parentheses.
SELECT CURRENT_TIME;19:07:32SELECT CURRENT_DATE;2001-02-17SELECT CURRENT_TIMESTAMP;2001-02-17 19:07:32-05

The functionnow() is the traditionalPostgres equivalent toCURRENT_TIMESTAMP.

There is alsotimeofday(), which returns current time to higher precision than theCURRENT_TIMESTAMP family does:

SELECT timeofday(); Sat Feb 17 19:07:32.000126 2001 EST

timeofday() uses the operating system callgettimeofday(2), which may have resolution as good as microseconds (depending on your platform); the other functions rely ontime(2) which is restricted to one-second resolution. For historical reasons,timeofday() returns its result as a text string rather than a timestamp value.

It is quite important to realize thatCURRENT_TIMESTAMP and related functions all return the time as of the start of the current transaction; their values do not increment while a transaction is running. Buttimeofday() returns the actual current time.

All the date/time datatypes also accept the special literal valuenow to specify the current date and time. Thus, the following three all return the same result:

SELECT CURRENT_TIMESTAMP;SELECT now();SELECT TIMESTAMP 'now';

Note: You do not want to use the third form when specifying a DEFAULT value while creating a table. The system will convertnow to a timestamp as soon as the constant is parsed, so that when the default value is needed, the time of the table creation would be used! The first two forms will not be evaluated until the default value is used, because they are function calls. Thus they will give the desired behavior of defaulting to the time of row insertion.

Notes

[1]

60 if leap seconds are implemented by the operating system


PrevHomeNext
Formatting FunctionsUpGeometric Functions and Operators

[8]ページ先頭

©2009-2025 Movatter.jp