Date/Time Functions and Operators v11
The Date/Time Functions table shows the available functions for date/time value processing, with details appearing in the following subsections. The following table illustrates the behaviors of the basic arithmetic operators (+, -)
. For formatting functions, refer to IMMUTABLE TO_CHAR(TIMESTAMP, format) Function. You should be familiar with the background 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 |
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 | Add 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 | Get 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 is 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 | Truncate according to format | TRUNC(TO_DATE ('29-MAY-05'), 'MON') | 01-MAY-05 00:00:00 |
ADD_MONTHS
The ADD_MONTHS
functions adds (or subtracts if the second parameter is negative) the specified number of months to the given date. 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 which 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 is carried forward to the result unchanged.
The following are examples of the ADD_MONTHS
function.
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 within 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 which is positive if the first date is greater than the second date or 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.
The following are some examples of the MONTHS_BETWEEN
function.
NEXT_DAY
The NEXT_DAY
function returns the first occurrence of the given weekday strictly greater than the given date. At least the first three letters of the weekday must be specified - e.g., SAT
. If the given date contains a time portion, it is carried forward to the result unchanged.
The following are examples of 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 |
Following is an example of the NEW_TIME
function:
ROUND
The ROUND
function returns a date rounded according to a specified template pattern. If the template pattern is omitted, 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 | Round to the nearest date that corresponds to the same day of the week as the first day of the year |
IW | Round to the nearest date that corresponds to the same day of the week as the first day of the ISO year |
W | Round 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 | Round to the nearest minute |
Following are examples of usage of the ROUND
function.
The following examples round to the nearest hundred years.
The following examples round to the nearest year.
The following examples round to the nearest ISO year. The first example rounds to 2004 and the ISO year for 2004 begins on December 29th of 2003. The second example rounds to 2005 and the ISO year for 2005 begins on January 3rd of that same year.
(An ISO year begins on the first Monday from which a 7 day span, Monday thru Sunday, contains at least 4 days of the new year. Thus, it is possible for the beginning of an ISO year to start in December of the prior year.)
The following example round to the nearest quarter:
The following example round to the nearest month:
The following examples round to the nearest week. The first day of 2007 lands on a Monday so in the first example, January 18th is closest to the Monday that lands on January 15th. In the second example, January 19th is closer to the Monday that falls on January 22nd.
The following 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.
The following examples round to the nearest week where a week is considered to start on the same day as the first day of the month.
The following examples round to the nearest day.
The following examples round to the start of the nearest day of the week (Sunday).
The following examples round to the nearest hour.
The following examples round to the nearest minute.
TRUNC
The TRUNC
function returns a date truncated according to a specified template pattern. If the template pattern is omitted, 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 of usage of the TRUNC
function.
The following example truncates down to the hundred years unit.
The following example truncates down to the year.
The following example truncates down to the beginning of the ISO year.
The following example truncates down to the start date of the quarter.
The following example truncates to the start of the month.
The following 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 19th is January 15th.
The following 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.
The following 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.
The following example truncates to the start of the day.
The following example truncates to the start of the week (Sunday).
The following example truncates to the start of the hour.
The following example truncates to the minute.
CURRENT DATE/TIME
Advanced Server provides a number of functions that return values related to the current date and time. These functions all 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
will not change if called multiple times within a transaction.
CURRENT_TIMESTAMP
returns the current date and time. When called from a single SQL statement, it will return the same value for each occurrence within the statement. If called from multiple statements within a transaction, may return different values for each occurrence. If called from a function, may return a different value than the value returned by current_timestamp in the caller.
LOCALTIMESTAMP
can optionally be given a precision parameter which causes the result to be rounded to that many 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 do not change during the transaction. This is considered a feature: the intent is to allow a single transaction to have a consistent notion of the “current” time, so that multiple modifications within the same transaction bear the same time stamp. Other database systems may advance these values more frequently.
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 be included in the result set. The valid interval types are DAY
, HOUR
, MINUTE
, and SECOND
.
The following example converts a numeric value to a time interval that includes days and hours:
The following 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 be included in the result set. The valid interval types are YEAR
and MONTH
.
The following example converts a numeric value to a time interval that includes years and months:
The following example converts a numeric value to a time interval that includes years only: