EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial Oracle Date Functions
 

Oracle Date Functions

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

Oracle Date Functions

 

 

Introduction to Oracle Date Functions

Date functions in Oracle can be defined as a set of functions which operate on date and allows the developer or users to retrieve the current date and time in a particular time zone or extract only the date/ month/year or more complex actions like extracting the last day of the month/ next day/ session time zone and it also consist of functions which can be used to convert a Date value to a character string or convert a date which is in character string to a Date value.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Oracle Date Functions with Examples

Given below are the various oracle date functions with examples:

1. CURRENT_DATE

This function is used to get the current date in the session time zone. It requires no parameters and is a very simple function.

Syntax:

CURRENT_DATE

Example:

In this example we will try to find the current date of the session using this function.

Code:

SELECT CURRENT_DATE FROM DUAL;

In the output we will see the current date of the session time zone.

2. SYSDATE

This function returns the current date and time of the Operating system in which the Oracle database is installed.

Syntax:

SYSDATE

Example:

In this example we are going to find the sysdate of the operating system in which the current database is installed.

Code:

select sysdate from dual;

Output:

oracle date function 1

As, we can see the screen shot shows us the system date.

3. EXTRACT

This extract function in Oracle is used to retrieve a specific component which can be year, day, month, hour, minute, second from a date value.

Syntax:

EXTRACT (component from source)

Parameters:

  • component: It refers to the component we want to extract (year, day, month, hour, minute, second).
  • source: It refers to the value (DATE, TIMESTAMP) from which we want to extract.

Example:

In this example we are going to extract the year from a date.

Code:

SELECT
EXTRACT( YEAR FROM TO_DATE( '29-Apr-2020 05:30:20 ',  'DD-Mon-YYYY HH24:MI:SS' ) ) YEAR
FROM
DUAL;

We have used to_date function.

Output:

oracle date function 2

In the above screen shot we can see that the year has been successfully extracted.

4. TO_DATE

This function converts a date which is in string type to date value. It takes three arguments.

Syntax:

TO_DATE(string, format, nls_language)

Parameters:

  • string: It refers to the date in string type which we want to convert.
  • format: It refers to the date and time format in which we want to convert and it is an optional parameter.
  • nls_language: It refers to the language for the day and month names. It is also an optional parameter.

Example:

In this example we will convert a date in string to date value.

Code:

SELECT
TO_DATE( '20 APR 2020', 'DD MON YYYY' )CONVERTED_DATE
FROM dual;

Output:

oracle date function 3

As we can see in the screen shot the DATE value has been converted into a specific format.

5. TO_CHAR

It is used to convert a date from DATE value to a specified date format.

Syntax:

TO_CHAR(expression, date_format)

Parameters:

  • expression: It refers to the DATE or an INTERVAL value which needs to be converted. The expression can be of type DATE OR TIMESTAMP
  • date_format: It refers to the specified format in which we are going to convert the expression. It is optional parameter.

Example:

In this example we are going to convert the system date or current date into a string value in a format DD-MM-YYYY.

Code:

SELECT
TO_CHAR( sysdate, 'DD-MM-YYYY' )NEW_DATE
FROM
dual;

Output:

TO_CHAR

As, we can see in the screen shot the sysdate has been converted in a specified format.

6. LAST_DAY

This function is used to return the last day of the month of the particular date. It takes a DATE argument as a parameter.

Syntax:

LAST_DAY(date)

Parameter:

  • date: This refers to the date value for which we want to get the last day of the month.

Example:

In this example we are going to extract the last day of the month of sysdate.

Code:

SELECT
LAST_DAY(sysdate) LAST_DAY
FROM
dual;

Output:

oracle date function 5

As we can see in the screen shot the query displays the last day of the month of April.

7. MONTHS_BETWEEN

This function is used to measure the months between two dates.

It takes two parameters as arguments.

Syntax:

MONTHS_BETWEEN(from_date, to_date)

Parameters:

  • from_date: It refers to the date which is subtracted from.
  • to_date: It refers to the date which is to be subtracted.

Example:

In this example we will calculate the months between system date and the date on which India won its second cricket world cup which was 2 April 2011.

Code:

SELECT
MONTHS_BETWEEN( sysdate, DATE '2011-04-02' ) MONTH_DIFFERENCE
FROM
DUAL;

Output:

MONTHS_BETWEEN

As we can see in the screen shot the query displays the months between the two dates.

8. ADD_MONTHS

This function adds N months to a date and returns the same day N month after.

Syntax:

ADD_MONTHS(expression, N)

Parameters:

  • expression: It refers to the date value.
  • N: It represents the number of months.

Example:

To get the today system day date after 2 months using the ADD_MONTHS function.

Code:

SELECT
ADD_MONTHS( sysdate, 2 ) NEWDATE
FROM
dual;

Output:

ADD_MONTHS

9. CURRENT_TIMESTAMP

This function returns the current date and time in the session time zone.

Syntax:

CURRENT_TIMESTAMP

Example:

Let us try to get the current time stamp of this particular session time zone.

Code:

SELECT
CURRENT_TIMESTAMP
FROM
dual;

Output:

CURRENT_TIMESTAMP

10. DBTIMEZONE

This represents the database time zone.

Syntax:

DBTIMEZONE

Example:

To get the current database time zone.

Code:

SELECT
DBTIMEZONE
FROM
dual;

Output:

DBTIMEZONE

11. FROM_TZ

This function converts the TIMESTAMP to TIMESTAMP with TIME ZONE value.

Syntax:

FROM_TZ(timestamp, timezone)

Parameters:

  • timestamp: It refers to the timestamp value.
  • timezone: It is a character string TZH:TZM.

Example:

To convert a timestamp to a timestamp with timezone value.

Code:

SELECT
FROM_TZ(TIMESTAMP '2020-05-01 19:35:10', '-07:00')NEWVALUE
FROM
DUAL;

Output:

oracle date function 10

12. NEW_TIME()

This function converts a date from one time zone to a different time zone.

Syntax:

NEW_TIME(date, from_timezone, new_timezone)

Parameters:

  • date: It refers to the date which we want to convert.
  • from_timezone: It refers to the time zone of the date.
  • new_timezone: It refers to the time zone to which we want to convert.

Code:

SELECT
NEW_TIME( sysdate,  'PST', 'AST' ) TIME_IN_AST
FROM
DUAL;

Output:

oracle date function 11

13. ROUND

This function rounds the date to a specific format.

Syntax:

ROUND(date, format)

Parameters:

  • date: It represents the date which we want to round.
  • format: It represents the format to which we want to round.

Example:

In this example we will round the current date 01-May-2020 20:27:15 to nearest date.

Code:

SELECT
TO_CHAR(
ROUND( TO_DATE( '01-May-2020 20:27:15',  'DD-Mon-YYYY HH24:MI:SS' ) ),
'DD-Mon-YYYY HH24:MI:SS' )  rounded_date
FROM
dual;

Output:

oracle date function 12

14. SESSIONTIMEZONE

This function as the name suggest returns the time zone of the current working session.

Syntax:

SESSIONTIMEZONE

Example:

To get the time zone of the current working session.

Code:

SELECT
SESSIONTIMEZONE
FROM
dual;

Output:

oracle date function 13

15. SYSTIMESTAMP

This function represents a timestamp with a time zone. It displays the result up to fractional seconds.

Syntax:

SYSTIMESTAMP

Example:

In this example we will try to get the system timestamp.

Code:

SELECT
SYSTIMESTAMP
FROM
dual;

Output:

oracle date function 14

16. TRUNC

This function returns a date value truncated to a specific format/unit.

Syntax:

TRUNC(date, format)

Parameters:

  • date: It represents the date value which is to be truncated.
  • format: It refers to the unit to which the date value will be truncated.

Example:

To get the first day of the current month.

Code:

SELECT
TRUNC( SYSDATE, 'MM' ) MONTH
FROM
dual;

Output:

oracle date function 15JPG

17. TZ_OFFSET

This function returns offset of a time zone name from UTC.

Syntax:

TZ_OFFSET(value)

Parameter:

  • value: It refers to a valid time zone.

Example:

We will get the offset of time zone ‘Asia/Kolkata.

Code:

SELECT
TZ_OFFSET('Asia/Kolkata') OFFSET
FROM
DUAL;

Output:

TZ_OFFSET

Conclusion

In this article we saw about the definition of Oracle Date functions and also about the various types of date functions available in the Oracle database. We saw different types of date functions their definitions, syntaxes and also with an example for each to better understand the functions individually.

Recommended Articles

This is a guide to Oracle Date Functions. Here we discuss the introduction to Oracle Date Functions with 17 functions and examples for better understanding. You may also have a look at the following articles to learn more –

  1. FETCH in Oracle
  2. Oracle UNIQUE Constraint
  3. Natural Join in Oracle
  4. Oracle Alter Table

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW