Date/time functions and operators v17
The date/time functions table shows the available functions for date/time value processing.
Overview of basic arithmetic operators
The following table shows the behaviors of the basic arithmetic operators (+, -)
. For formatting functions, refer to IMMUTABLE TO_CHAR(TIMESTAMP, format). For more information on date/time data types, see Date/time types.
Operator | Example | Result |
---|---|---|
plus (+) | DATE '2001-09-28' + 7 | 05-OCT-01 00:00:00 |
plus (+) | TIMESTAMP '2001-09-28 13:30:00' + 3 | 01-OCT-01 13:30:00 |
minus (-) | DATE '2001-10-01' – 7 | 24-SEP-01 00:00:00 |
minus (-) | TIMESTAMP '2001-09-28 13:30:00' - 3 | 25-SEP-01 13:30:00 |
minus (-) | TIMESTAMP '2001-09-29 03:00:00' - TIMESTAMP '2001-09-27 12:00:00' | @ 1 day 15 hours |
Overview of date and time functions
In the date/time functions of the following table the use of the DATE
and TIMESTAMP
data types are interchangeable.
Function | Return type | Description | Example | Result |
---|---|---|---|---|
ADD_MONTHS (DATE, NUMBER) | DATE | Adds months to a date. | ADD_MONTHS ('28-FEB-97', ,3.8) | 31-MAY-97 00:00:00 |
CURRENT_DATE | DATE | Current date. | CURRENT_DATE | 04-JUL-07 |
CURRENT_TIMESTAMP | TIMESTAMP | Returns the current date and time. | CURRENT_TIMESTAMP | 04-JUL-07 15:33:23.484 |
EXTRACT(field FROM TIMESTAMP) | DOUBLE PRECISION | Gets subfield. | EXTRACT(hour FROM TIMESTAMP '2001-02-16 20:38:40') | 20 |
LAST_DAY(DATE) | DATE | Returns the last day of the month represented by the given date. If the given date contains a time portion, it's carried forward to the result unchanged. | LAST_DAY('14-APR-98') | 30-APR-98 00:00:00 |
LOCALTIMESTAMP [ (precision) ] | TIMESTAMP | Current date and time (start of current transaction). | LOCALTIMESTAMP | 04-JUL-07 15:33:23.484 |
MONTHS_BETWEEN (DATE, DATE) | NUMBER | Number of months between two dates. | MONTHS_BETWEEN ('28-FEB-07', '30-NOV-06') | 3 |
NEXT_DAY(DATE, dayofweek) | DATE | Date falling on dayofweek following specified date. | NEXT_DAY('16-APR-07',FRI' ) | 20-APR-07 00:00:00 |
NEW_TIME(DATE, VARCHAR, VARCHAR) | DATE | Converts a date and time to an alternate time zone. | NEW_TIME (TO_DATE '2005/05/29 01:45', 'AST', 'PST') | 2005/05/29 21:45:00 |
NUMTODSINTERVAL(NUMBER, INTERVAL) | INTERVAL | Converts a number to a specified day or second interval. | SELECT numtodsinterval(100, ‘hour’); | 4 days 04:00:00 |
NUMTOYMINTERVAL(NUMBER, INTERVAL) | INTERVAL | Converts a number to a specified year or month interval. | SELECT numtoyminterval(100, ‘month’); | 8 years 4 mons |
ROUND(DATE [, format ]) | DATE | Date rounded according to format . | ROUND(TO_DATE('29-MAY-05'),'MON') | 01-JUN-05 00:00:00 |
SYS_EXTRACT_UTC(TIMESTAMP WITH TIME ZONE) | TIMESTAMP | Returns Coordinated Universal Time. | SYS_EXTRACT_UTC(CAST ('24-MAR-11 12:30:00PM -04:00' AS TIMESTAMP WITH TIME ZONE)) | 24-MAR-11 16:30:00 |
SYSDATE | DATE | Returns current date and time. | SYSDATE | 01-AUG-12 11:12:34 |
SYSTIMESTAMP() | TIMESTAMP | Returns current date and time. | SYSTIMESTAMP | 01-AUG-12 11:11:23. 665229 -07:00 |
TRUNC(DATE [format]) | DATE | Truncates according to format . | TRUNC(TO_DATE ('29-MAY-05'), 'MON') | 01-MAY-05 00:00:00 |
DBTIMEZONE | [+|-]TZH:TZM or time zone region name | Returns time zone offset in the format [+|-]TZH:TZM or time zone region name. | DBTIMEZONE | -9:00 or Europe/London |
SESSIONTIMEZONE | [+|-]TZH:TZM or time zone region name | Returns time zone offset in the format [+|-]TZH:TZM or time zone region name. | SESSIONTIMEZONE | -9:00 or Europe/London |
ADD_MONTHS
The ADD_MONTHS
functions adds or subtracts the specified number of months to or from the given date. Use a negative value to subtract. The resulting day of the month is the same as the day of the month of the given date except when the day is the last day of the month. In that case, the resulting date always falls on the last day of the month.
Any fractional portion of the number of months parameter is truncated before performing the calculation.
If the given date contains a time portion, it's carried forward to the result unchanged.
These examples show the ADD_MONTHS
function:
CURRENT DATE/TIME
EDB Postgres Advanced Server provides functions that return values related to the current date and time. These functions return values based on the start time of the current transaction:
CURRENT_DATE
CURRENT_TIMESTAMP
LOCALTIMESTAMP
LOCALTIMESTAMP(precision)
CURRENT_DATE
returns the current date and time based on the start time of the current transaction. The value of CURRENT_DATE
doesn't change if called multiple times in a transaction.
CURRENT_TIMESTAMP
returns the current date and time. When called from a single SQL statement, it returns the same value for each occurrence in the statement. If called from multiple statements in a transaction, it might return different values for each occurrence. If called from a function, it might return a value different from the one returned by current_timestamp
in the caller.
You can optionally give LOCALTIMESTAMP
a precision parameter. The parameter causes the result to be rounded to the specified number of fractional digits in the seconds field. Without a precision parameter, the result is given to the full available precision.
Since these functions return the start time of the current transaction, their values don't change during the transaction. The intent is to allow a single transaction to have a consistent notion of the “current” time, so that multiple modifications in the same transaction have the same timestamp. Other database systems might advance these values more frequently.
TIMEZONE functions
EDB Postgres Advanced Server provides functions that return timezone set values.
DBTIMEZONE
DBTIMEZONE
returns the value of the database time zone. The return type is a time zone offset in the format [+\|-]TZH:TZM
or a time zone region name. The return type depends on the database time zone value specified in the recent CREATE DATABASE
or ALTER DATABASE
statement.
SESSIONTIMEZONE
SESSIONTIMEZONE
returns the time zone of the current session. The return type is a time zone offset in the format [+\|-]TZH:TZM
or a time zone region name. The return type depends on the session time zone value specified in the recent ALTER SESSION
or SET
statement.
EXTRACT
The EXTRACT
function retrieves subfields such as year or hour from date/time values. The EXTRACT
function returns values of type DOUBLE PRECISION
. The following are valid field names:
YEAR
The year field.
MONTH
The number of the month in the year (1 - 12).
DAY
The day of the month field (1 - 31).
HOUR
The hour field (0 - 23).
MINUTE
The minutes field (0 - 59).
SECOND
The seconds field, including fractional parts (0 - 59).
MONTHS_BETWEEN
The MONTHS_BETWEEN
function returns the number of months between two dates. The result is a numeric value that's positive if the first date is greater than the second date. It's negative if the first date is less than the second date.
The result is always a whole number of months if the day of the month of both date parameters is the same or both date parameters fall on the last day of their respective months.
These examples show the MONTHS_BETWEEN
function:
NEXT_DAY
The NEXT_DAY
function returns the first occurrence of the given weekday strictly greater than the given date. You must specify at least the first three letters of the weekday, for example, SAT
. If the given date contains a time portion, it's carried forward to the result unchanged.
These examples show the NEXT_DAY
function.
NEW_TIME
The NEW_TIME
function converts a date and time from one time zone to another. NEW_TIME
returns a value of type DATE
. The syntax is:
time_zone1
and time_zone2
must be string values from the Time zone column of the following table.
Time zone | Offset from UTC | Description |
---|---|---|
AST | UTC+4 | Atlantic Standard Time |
ADT | UTC+3 | Atlantic Daylight Time |
BST | UTC+11 | Bering Standard Time |
BDT | UTC+10 | Bering Daylight Time |
CST | UTC+6 | Central Standard Time |
CDT | UTC+5 | Central Daylight Time |
EST | UTC+5 | Eastern Standard Time |
EDT | UTC+4 | Eastern Daylight Time |
GMT | UTC | Greenwich Mean Time |
HST | UTC+10 | Alaska-Hawaii Standard Time |
HDT | UTC+9 | Alaska-Hawaii Daylight Time |
MST | UTC+7 | Mountain Standard Time |
MDT | UTC+6 | Mountain Daylight Time |
NST | UTC+3:30 | Newfoundland Standard Time |
PST | UTC+8 | Pacific Standard Time |
PDT | UTC+7 | Pacific Daylight Time |
YST | UTC+9 | Yukon Standard Time |
YDT | UTC+8 | Yukon Daylight Time |
This example shows the NEW_TIME
function:
NUMTODSINTERVAL
The NUMTODSINTERVAL
function converts a numeric value to a time interval that includes day-through-second interval units. When calling the function, specify the smallest fractional interval type to include in the result set. The valid interval types are DAY
, HOUR
, MINUTE
, and SECOND
.
This example converts a numeric value to a time interval that includes days and hours:
This example converts a numeric value to a time interval that includes minutes and seconds:
NUMTOYMINTERVAL
The NUMTOYMINTERVAL
function converts a numeric value to a time interval that includes year-through-month interval units. When calling the function, specify the smallest fractional interval type to include in the result set. The valid interval types are YEAR
and MONTH
.
This example converts a numeric value to a time interval that includes years and months:
This example converts a numeric value to a time interval that includes years only:
ROUND
The ROUND
function returns a date rounded according to a specified template pattern. If you omit the template pattern, the date is rounded to the nearest day. The following table shows the template patterns for the ROUND
function.
Pattern | Description |
---|---|
CC, SCC | Returns January 1, cc 01 where cc is first 2 digits of the given year if last 2 digits <= 50, or 1 greater than the first 2 digits of the given year if last 2 digits > 50; (for AD years) |
SYYY, YYYY, YEAR, SYEAR, YYY, YY, Y | Returns January 1, yyyy where yyyy is rounded to the nearest year; rounds down on June 30, rounds up on July 1 |
IYYY, IYY, IY, I | Rounds to the beginning of the ISO year, which is determined by rounding down if the month and day is on or before June 30th or by rounding up if the month and day is July 1st or later |
Q | Returns the first day of the quarter determined by rounding down if the month and day is on or before the 15th of the second month of the quarter or by rounding up if the month and day is on the 16th of the second month or later of the quarter |
MONTH, MON, MM, RM | Returns the first day of the specified month if the day of the month is on or prior to the 15th; returns the first day of the following month if the day of the month is on the 16th or later |
WW | Rounds to the nearest date that corresponds to the same day of the week as the first day of the year |
IW | Rounds to the nearest date that corresponds to the same day of the week as the first day of the ISO year |
W | Rounds to the nearest date that corresponds to the same day of the week as the first day of the month |
DDD, DD, J | Rounds to the start of the nearest day; 11:59:59 AM or earlier rounds to the start of the same day; 12:00:00 PM or later rounds to the start of the next day |
DAY, DY, D | Rounds to the nearest Sunday |
HH, HH12, HH24 | Round to the nearest hour |
MI | Rounds to the nearest minute |
These examples show the use of the ROUND
function.
These examples round to the nearest hundred years:
These examples round to the nearest year:
These examples round to the nearest ISO year. The first example rounds to 2004, and the ISO year for 2004 begins on December 29, 2003. The second example rounds to 2005, and the ISO year for 2005 begins on January 3 of that same year.
(An ISO year begins on the first Monday from which a seven-day span, Monday through Sunday, contains at least four days of the new year. It's possible for the beginning of an ISO year to start in December of the prior year.)
These examples round to the nearest quarter:
These examples round to the nearest month:
These examples round to the nearest week. The first day of 2007 lands on a Monday. In the first example, January 18 is closest to the Monday that lands on January 15. In the second example, January 19 is closer to the Monday that falls on January 22.
These examples round to the nearest ISO week. An ISO week begins on a Monday. In the first example, January 1, 2004 is closest to the Monday that lands on December 29, 2003. In the second example, January 2, 2004 is closer to the Monday that lands on January 5, 2004.
These examples round to the nearest week, where a week is considered to start on the same day as the first day of the month:
These examples round to the nearest day:
These examples round to the start of the nearest day of the week (Sunday):
These examples round to the nearest hour:
These examples round to the nearest minute:
SYSDATE
The SYSDATE
function returns the current date and time (timestamp without timezone) of the operating system on which the database server resides. The function is STABLE
and requires no arguments.
When called from a single SQL statement, it returns the same value for each occurrence in the statement. If called from multiple statements in a transaction, it might return different values for each occurrence. If called from a function, it might return a value different from the one returned by SYSDATE
in the caller.
This example shows a call to SYSDATE
:
TRUNC
The TRUNC
function returns a date truncated according to a specified template pattern. If you omit the template pattern, the date is truncated to the nearest day. The following table shows the template patterns for the TRUNC
function.
Pattern | Description |
---|---|
CC, SCC | Returns January 1, cc 01 where cc is first 2 digits of the given year |
SYYY, YYYY, YEAR, SYEAR, YYY, YY, Y | Returns January 1, yyyy where yyyy is the given year |
IYYY, IYY, IY, I | Returns the start date of the ISO year containing the given date |
Q | Returns the first day of the quarter containing the given date |
MONTH, MON, MM, RM | Returns the first day of the specified month |
WW | Returns the largest date just prior to or the same as the given date that corresponds to the same day of the week as the first day of the year |
IW | Returns the start of the ISO week containing the given date |
W | Returns the largest date just prior to, or the same as the given date that corresponds to the same day of the week as the first day of the month |
DDD, DD, J | Returns the start of the day for the given date |
DAY, DY, D | Returns the start of the week (Sunday) containing the given date |
HH, HH12, HH24 | Returns the start of the hour |
MI | Returns the start of the minute |
Following are examples that use the TRUNC
function.
This example truncates down to the hundred-years unit:
This example truncates down to the year:
This example truncates down to the beginning of the ISO year:
This example truncates down to the start date of the quarter:
This example truncates to the start of the month:
This example truncates down to the start of the week determined by the first day of the year. The first day of 2007 lands on a Monday, so the Monday just prior to January 19 is January 15.
This example truncates to the start of an ISO week. An ISO week begins on a Monday. January 2, 2004 falls in the ISO week that starts on Monday, December 29, 2003.
This example truncates to the start of the week, where a week is considered to start on the same day as the first day of the month:
This example truncates to the start of the day:
This example truncates to the start of the week (Sunday):
This example truncates to the start of the hour:
This example truncates to the minute: