Updated April 6, 2023
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:
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.
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 –
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 –
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 –
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 –
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
We hope that this EDUCBA information on “PL/SQL to_DATE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.