Updated April 5, 2023
Introduction to PLSQL format date
PL/ SQL format date is used for specifying the structure in which the value of the date is being stored, retrieved, or manipulated in PL/ SQL. The date data type is used for storing the date as well as time-related value in it. The value stored in PL/ SQL date format is the point in time and has the precision of one second. In this article, we will study the default format of the date in PL/ SQL, contents being stored in date filed, and how we can check and change the default format along with the help of certain examples.
The date format
By default, the format in which the date is being stored in the date datatype field is in DD-MON-YY format where DD is for date, MON is for the first three initial characters of the month and YY is the last two digits of the year. This default format is controlled by a parameter named NLS_DATE_FORMAT. If you want to retrieve the default format from the system in PL/SQL then we can make the use of following query statement –
SELECT
value
FROM
V$NLS_PARAMETERS
WHERE
parameter = 'NLS_DATE_FORMAT';
The output of the above query statement is as shown below showing the default date format as DD-MON-YY for date datatype –
The values that are stored in the date field include year, month, day, hours, minutes, and seconds. The range of date values that can be stored in date format field in PL/ SQL is 1 January BCE that is before Christ era to 31 December 9999 CE that is a common era or A.D. The default value used in PL/ SQL for storing date value is CE in case if we have not specified BCE explicitly in date.
Retrieving current date
If you want to retrieve the current date value in PL/ SQL in the default format, then you can make the use of sysdate function. In order to demonstrate to you how we can retrieve it, you can make the use of following query statement
SELECT sysdate FROM dual;
The output of the above query statement will fetch the value of the current date in the standard format as shown below. The output may differ depending on the current system date of your machine.
Changing the date format
You can change the standard default date format by changing and altering the value of the parameter NLS_DATE_FORMAT. Consider one example where you want to change the value of the date format to ‘DD-MM-YYYY’. In such case, you can make the use of ALTER SESSION statement which can change the value of the parameter NLS_DATE_FORMAT as shown in the below query statement –
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY';
The output of the above query statement will become as follows –
In order to check if the format is changed for standard or default format of date, you can fire the same command which retrieves the value of sysdate for current system standard date by using the below query statement –
SELECT sysdate FROM dual;
The output of the above query statement will fetch the value of the current date in the updated standard format as shown below which means that the default format of the date has been changed properly. The output may differ depending on the current system date of your machine.
Format specification
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 date is considered as DD-MON-YY for example – 26-JAN-1996.
Using TO_CHAR () function for format specification –
TO_CHAR () is the function that can be used to change the format of the date value passed to it in the format which is expected in the resultant date. So basically, we can say that in order to change the format of specific date value in PL/ SQL, we can make the use of TO_CHAR () function. The specification of the expected format should be done considering the conventions mentioned in the above table. Consider the following example for demonstration of TO_CHAR () date formatting function –
SELECT
TO_CHAR ('16-JUN-21', 'FMMonth DD, YYYY')
FROM
dual;
The output of the above query statement is as shown below –
NLS_DATE_LANGUAGE parameter controls the language in which the TO_CHAR () function returns the date value. In order to verify and check the language of the TO_CHAR () function, we can make the use of following query statement –
SELECT
value
FROM
V$NLS_PARAMETERS
WHERE
parameter = 'NLS_DATE_LANGUAGE';
The output of the above query statement is as shown below retrieving the output language as American –
We can even alter this language in which the date format used by TO_CHAR () is changing the date –
ALTER SESSION SET NLS_DATE_LANGUAGE = 'FRENCH';
The output of the above query statement is as shown below –
In order to check whether the language of date format has been changed in PL/ SQL for standard or default format, you can try to execute the same query again which is used for fetching the language of date as shown below –
SELECT
value
FROM
V$NLS_PARAMETERS
WHERE
parameter = 'NLS_DATE_LANGUAGE';
The output of the above query statement is as shown below retrieving the output updated language as French –
Conclusion – PLSQL format date
The default standard format of date used in PL/ SQL is DD-MON-YY which specifies date month and year in the format as example 01-JUN-21. This date format can also be modified further by using the ALTER SESSION statement and changing the parameter value o NLS_DATE_FORMAT. We can also retrieve a particular date other than the default format by making the use of TO_CHAR () function in PLSQL by specifying the expected format and the date value as its arguments.
Recommended Articles
We hope that this EDUCBA information on “PLSQL format date” was beneficial to you. You can view EDUCBA’s recommended articles for more information.