Oracle Date-Time Functions Version 21c |
---|
General Information | ||||||||||||||||||||||||||
Library Note |
|
|||||||||||||||||||||||||
Purpose | This page is a collection of built-in Oracle Database functions used to evaluate and manipulate dates. There are additional functions not listed here such as CASE and CAST you will find links to them at the bottom of the page. Additional undocumented string related functions can be found on the Undocumented Oracle page also linked at page bottom. |
|||||||||||||||||||||||||
Note |
|
|||||||||||||||||||||||||
Dependencies |
|
|||||||||||||||||||||||||
+ AND - | ||||||||||||||||||||||||||
+ | <date> + <integer> |
|||||||||||||||||||||||||
SELECT SYSDATE + 1 FROM dual; |
||||||||||||||||||||||||||
- | <date> - <integer> |
|||||||||||||||||||||||||
SELECT SYSDATE - 1 FROM dual; |
||||||||||||||||||||||||||
ADD_MONTHS | ||||||||||||||||||||||||||
Add A Month To A Date | ADD_MONTHS(left IN DATE, right IN NUMBER) RETURN DATE; |
|||||||||||||||||||||||||
SELECT add_months(SYSDATE, 2) FROM dual; |
||||||||||||||||||||||||||
APPROX_MEDIAN | ||||||||||||||||||||||||||
Returns absolutely nothing because the Oracle Docs are incorrect. Here's what they state: "
It takes a numeric or datetime value and returns an approximate middle value or an approximate interpolated value...." Take a look at my demo: I say they are incorrect. This is documented to help those that wonder what's wrong after reading the docs. |
APPROX_MEDIAN(<expression> [DETERMINISTIC] [, <ERROR_RATE> | <CONFIDENCE>']) RETURN DATE; |
|||||||||||||||||||||||||
SQL> SELECT default_tablespace, APPROX_MEDIAN(created DETERMINISTIC) ORAFAIL |
||||||||||||||||||||||||||
CURRENT_DATE | ||||||||||||||||||||||||||
Returns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE | CURRENT_DATE RETURN DATE; |
|||||||||||||||||||||||||
col sessiontimezone format a30 |
||||||||||||||||||||||||||
DUMP | ||||||||||||||||||||||||||
Returns a VARCHAR2 value containing the datatype code, length in bytes, and internal representation of a value | DUMP( |
|||||||||||||||||||||||||
col drows format a40 |
||||||||||||||||||||||||||
EXTRACT | ||||||||||||||||||||||||||
Extracts and returns the value of a specified datetime field from a datetime or interval value expression | Values That Can Be Extracted:YEAR |
|||||||||||||||||||||||||
SELECT EXTRACT(YEAR FROM DATE '2009-03-15') FROM dual; |
||||||||||||||||||||||||||
GREATEST | ||||||||||||||||||||||||||
Return the Latest Date | GREATEST(pattern IN DATE) RETURN DATE; |
|||||||||||||||||||||||||
CREATE TABLE t ( |
||||||||||||||||||||||||||
INTERVAL | ||||||||||||||||||||||||||
Interval to adjust date-time | INTERVAL '<integer>' <unit> |
|||||||||||||||||||||||||
SELECT TO_CHAR(SYSDATE, 'HH:MI:SS') |
||||||||||||||||||||||||||
LAST_DAY | ||||||||||||||||||||||||||
Returns The Last Date Of A Month | LAST_DAY(right IN DATE) RETURN DATE; |
|||||||||||||||||||||||||
SELECT * FROM t; |
||||||||||||||||||||||||||
LEAST | ||||||||||||||||||||||||||
Return the Earliest Date | LEAST(pattern IN DATE) RETURN DATE; |
|||||||||||||||||||||||||
SELECT * FROM t; |
||||||||||||||||||||||||||
LENGTH | ||||||||||||||||||||||||||
Returns length in characters | LENGTH(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT LENGTH(last_ddl_time) FROM user_objects; |
||||||||||||||||||||||||||
LENGTHB | ||||||||||||||||||||||||||
Returns length in bytes | LENGTHB(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT LENGTHB(last_ddl_time) FROM user_objects; |
||||||||||||||||||||||||||
LENGTHC | ||||||||||||||||||||||||||
Returns length in characters | LENGTHC(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT LENGTHC(last_ddl_time) FROM user_objects; |
||||||||||||||||||||||||||
LENGTH2 | ||||||||||||||||||||||||||
Returns length in UCS2 code points | LENGTH2(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT LENGTH2(last_ddl_time) FROM user_objects; |
||||||||||||||||||||||||||
LENGTH4 | ||||||||||||||||||||||||||
Returns length in UCS4 code points | LENGTH4(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT LENGTH4(last_ddl_time) FROM user_objects; |
||||||||||||||||||||||||||
MAX | ||||||||||||||||||||||||||
Return the Latest Date | MAX(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT * FROM t; |
||||||||||||||||||||||||||
MIN | ||||||||||||||||||||||||||
Return the Earliest Date | MIN(<date>) RETURN INTEGER; |
|||||||||||||||||||||||||
SELECT * FROM t; |
||||||||||||||||||||||||||
MONTHS_BETWEEN | ||||||||||||||||||||||||||
Returns The Months Separating Two Dates | MONTHS_BETWEEN(left IN DATE, right IN DATE) RETURN NUMBER; |
|||||||||||||||||||||||||
SELECT MONTHS_BETWEEN(SYSDATE+365, SYSDATE-365) FROM dual; |
||||||||||||||||||||||||||
NEW_TIME | ||||||||||||||||||||||||||
Returns the date and time in time zone zone2 when date and time in time zone zone1 are date. Before using this function, you must set the NLS_DATE_FORMAT parameter to display 24-hour time. |
NEW_TIME(right IN DATE, middle IN VARCHAR2, left IN VARCHAR2) RETURN DATE; |
|||||||||||||||||||||||||
SELECT NEW_TIME(TO_DATE('03-15-2020 01:23:45', |
||||||||||||||||||||||||||
NEXT_DAY | ||||||||||||||||||||||||||
Date of next specified date following a date | NEXT_DAY(left IN DATE, right IN VARCHAR2) RETURN DATE; |
|||||||||||||||||||||||||
SELECT NEXT_DAY(SYSDATE, 'FRI') FROM dual; |
||||||||||||||||||||||||||
ORA_DST_AFFECTED | ||||||||||||||||||||||||||
Takes a datetime expression and returns 1 if the datetime value is affected by or will result in a "nonexisting time" or "duplicate time" error with the new time zone data: Otherwise 0 | ORA_DST_AFFECTED(<date_time_expression>; |
|||||||||||||||||||||||||
Follow DBMS_DST link at page bottom |
||||||||||||||||||||||||||
ORA_DST_CONVERT | ||||||||||||||||||||||||||
Allows specification of error handling for a specified datetime expression | ORA_DST_CONVERT(<date_time_expression>; |
|||||||||||||||||||||||||
Follow DBMS_DST link at page bottom |
||||||||||||||||||||||||||
ORA_DST_ERROR | ||||||||||||||||||||||||||
Resolves at datatime expression to a TIMESTAMP WITH TIME ZONE value or a VARRAY with TIMESTAMP WITH TIME ZONE values | ORA_DST_ERROR(<date_time_expression>; |
|||||||||||||||||||||||||
Follow DBMS_DST link at page bottom |
||||||||||||||||||||||||||
ROUND | ||||||||||||||||||||||||||
Returns date rounded to the unit specified by the format. If no format, the date is rounded to the nearest day Overload 1 |
ROUND(right IN DATE) RETURN DATE; |
|||||||||||||||||||||||||
SELECT ROUND(TO_DATE('10-FEB-20 10:10:10', 'DD-MON-RR HH24:MI:SS')) NEW_YEAR |
||||||||||||||||||||||||||
Returns date rounded to the unit specified by the format model. If you omit the format, the date is rounded to the nearest day Overload 2 |
ROUND( |
|||||||||||||||||||||||||
SELECT ROUND(TO_DATE('27-JAN-2020'),'YEAR') NEW_YEAR |
||||||||||||||||||||||||||
Spelled Out Using TO_CHAR | ||||||||||||||||||||||||||
Spelled Out Demo |
|
|||||||||||||||||||||||||
SELECT TO_CHAR(TO_DATE('10:30:18', 'HH24:MI:SS'), 'HH24SP:MISP:SSSP') |
||||||||||||||||||||||||||
SYSDATE | ||||||||||||||||||||||||||
Returns the current date and time set for the operating system on which the database resides | SYSDATE RETURN DATE; |
|||||||||||||||||||||||||
SELECT SYSDATE FROM dual; |
||||||||||||||||||||||||||
TO_CHAR | ||||||||||||||||||||||||||
Converts a date to a formatted string |
|
|||||||||||||||||||||||||
SELECT TO_CHAR(CURRENT_DATE, 'DD-MON-YYYY HH:MI:SS') FROM dual; |
||||||||||||||||||||||||||
TRUNC | ||||||||||||||||||||||||||
Convert a date to the date at midnight Overload 1 |
TRUNC(left IN DATE) RETURN DATE; |
|||||||||||||||||||||||||
conn uwclass/uwclass@pdbdev |
||||||||||||||||||||||||||
Selectively remove part of the date information Overload 2 Special thanks to Dave Hayes for reminding me of this. |
TRUNC(left IN DATE, right IN VARCHAR2) RETURN DATE; |
|||||||||||||||||||||||||
SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH:MI:SS') |
||||||||||||||||||||||||||
Dates in WHERE Clause Joins | conn uwclass/uwclass@pdbdev |
|||||||||||||||||||||||||
VSIZE | ||||||||||||||||||||||||||
Returns The Number Of Bytes Required By A Value | VSIZE(e IN DATE) RETURN NUMBER; |
|||||||||||||||||||||||||
SELECT VSIZE(SYSDATE) FROM dual; |
||||||||||||||||||||||||||
Date Calculations | ||||||||||||||||||||||||||
Returns A Day A Specified Number Of Days In The Future Skipping Weekends | CREATE OR REPLACE FUNCTION business_date (start_date DATE, Days2Add NUMBER) |
|||||||||||||||||||||||||
Business Date function, above, enhanced by Larry Benton to handle negative values for the days2add parameter. | CREATE OR REPLACE FUNCTION business_date (start_date DATE, days2add NUMBER) |
|||||||||||||||||||||||||
Returns The First Day Of A Month | CREATE OR REPLACE FUNCTION fday_ofmonth(value_in DATE) |
|||||||||||||||||||||||||
Time Calculations | ||||||||||||||||||||||||||
Returns The Number Of Seconds Between Two Date-Time Values | CREATE OR REPLACE FUNCTION time_diff (DATE_1 IN DATE, DATE_2 IN DATE) |
|||||||||||||||||||||||||
Calculate time from seconds Posted by John K. Hinsdale 12/30/06 to c.d.o.misc |
SELECT DECODE(FLOOR(999999/86400), 0, '', FLOOR(999999/86400) || ' day(s), ') || |
|||||||||||||||||||||||||
Obtain counts per ten minute increment Posted by Michele Cadot 03/09/08 to c.d.o.misc |
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS'; |
This site is maintained by Dan Morgan. Last Updated: | This site is protected by copyright and trademark laws under U.S. and International law. © 1998-2023 Daniel A. Morgan All Rights Reserved | |||||||||
|
||||||||||