EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials PL/SQL Tutorial PL/SQL to_DATE
Secondary Sidebar
Install Skype on Ubuntu

Java Books

Ubuntu Budgie

Address Binding in an Operating System

Normal and Trace of a Matrix in Java

Preemptive vs Non-Preemptive Scheduling

PL/SQL to_DATE

PL/SQL to_DATE

Introduction to PL/SQL to_DATE

PL/ SQL to_date function is used to convert the date value specified in any string kind of datatypes such as varchar, varchar2, char, or char2 to the date data type in PL/ SQL, which is in the standard date format as supported in PL/ SQL. When we store the data in a relational database by using the PL/ SQL DBMS, we need to store multiple values, which are of many kinds.

One of them is the date value used to store the date-related values like timestamp, joining date, leaving date, release date, publish date, etc. For this, we need to be very careful that the data which w are trying to insert should be of the same date format as defined by the data type of that column. For this, we can make use of the to_date function in PL/ SQL.

In this article, we will study the general syntax, usage, and implementation along with certain examples.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

We can make the use of to_date function in the following versions of Pl/SQL in oracle –

Oracle 8i, Oracle 11g, Oracle 9i, Oracle 10g, and Oracle 12c.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,629 ratings)

The syntax of the to_date() function is as shown below –

TO_DATE(source string [, mask or format][, nls_language])

In the above syntax, the arguments that are used are as described below –

  • Source string – This is any value in the string format that can be a string literal value or column value of a particular table and can be in CHAR, NCHAR, or VARCHAR2, NVARCHAR2 datatype.
  • Mask or format – This is an optional parameter. It is basically used to specify which format of the date is followed by the string value being supplied that follows the conventions as shown in the below table. We can use punctuation marks such as period(.), slash(/), comma(,), colon(:) and hyphen(-).
Element Details
DAY Name of the day of the week in which the current date belongs, which is being controlled by NLS_DATE_LANGUAGE.
DD Day of the month specified in the date.
HH Hour of the day
MONTH Name of the month value for date being supplied
YYYY 4 digit year value

By default, when this format is not specified, then the format for the date source string is considered as DD-MON-YY, for example – 26-JAN-1996. In case if the format or mask value passed in the second parameter is specified as J, which is Julien, then the source string supplied must be an integer value.

Nls_language – It is an expression which can help in specifying the language of month or day value in the source string. This parameter is supplied by using the below format –

NLS_DATE_LANGUAGE = language to be supplied

Return value – The output value being returned by the TO_DATE function is the date value that represents and corresponds to the date which is supplied in the source string.

Examples of PL/SQL to_DATE

Let us firstly consider an example where we will try to convert a particular string value containing the date inside it to the date format such that it will have YYYY as the year value in 4 digits, MM month value in 2 digits, and the DD for the date value of that month.

Consider that we have the string literal value a “26 Jan 1996”. If we want to convert this string literal into its corresponding date value, then we must first try to construct that date format and mask to specify it in the second argument as this will be the format of the input source string date value. Hence if we pass both this values as the parameter to the TO_DATE() function, then our query statement will become as shown in the below statement-

SELECT TO_DATE(’26 Jan 1996’,’ DD MON YYYY’) FROM dual;

The output of the above query statement is as shown below –

PL SQL date 1

If we even do a single mistake in specifying the format or mask-like suppose that we specify only MO instead of MON, then the query statement will become as shown below, and the output will produce an error as shown in its output –

SELELCT TO_DATE('26 Jan 1996','DD MO YYYY') FROM dual;

The output of the above query statement is as shown below, showing an error saying that the format is ending even before finishing the conversion of the string to the date value –

PL SQL date 2

We can also use the TO_DATE function to store the values in the table while inserting them. This is the most frequent place where we make use of the TO_DATE() function. As there might be many situations where we are not sure if the value coming from the application which needs to be stored in a database is in string format or date, we need to convert it into the date format firstly before we insert that value in the column of the table having the datatype mentioned as DATE.

Consider that we have one table named writers, which is created by using the following query statement –

CREATE TABLE writers (
writer_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
f_name VARCHAR2 ( 50 ) NOT NULL,
l_name VARCHAR2 ( 50 ) NOT NULL,
joining_date DATE NOT NULL,
PRIMARY KEY ( writer_id )
);

The output of the above code is as shown below –

PL SQL date 3

Now, whenever we want to insert the value in the writer’s table then while specifying the value of the joining date column, we should make the use of TO_DATE() function as shown in the below query statement –

INSERT INTO writers(f_name, l_name, joining_date)
VALUES ('Mayur','Sachwani', TO_DATE('Jan 03 1994','Mon DD YYYY'));

In the above statement, the use of the TO_DATE function is necessary because the format MON DD YYYY is not supported by the standard oracle date formats. The output of the above code is as shown below –

PL/SQL to_DATE

You can also check the contents of the writer’s table by making the use of the SELECT query statement.

Conclusion

TO_DATE() function is used in PL/ SQL to convert the value of string format date having datatype like CHAR, VARCHAR2, NCHAR, NVARCHAR2 to the standard date format supported by PL/ SQL. We need to specify the format or mask in which we are supplying the source date value if it’s not specified in the default format.

Recommended Articles

This is a guide to PL/SQL to_DATE. Here we discuss the Introduction, Syntax, parameters, examples with code implementation. You may also have a look at the following articles to learn more –

  1. Triggers in PL/SQL
  2. PL/SQL Collections
  3. Loops in PL/SQL
  4. PL/SQL Data Types
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more