Updated March 4, 2023
Definition of Oracle Date Format
Oracle provides the different function and format to the user, from the different format we have a date format, oracle database handles the date format in a straightforward way and it is very easy to understand, but many client and PL/SQL developers have confusion about the data format. The biggest problem is that they don’t know how dates are stored in the database, which is the correct format of date. So that reason date format command assign standard format to the object and it has different types such AS DATETIME, TIMESTAMP, TIMESTAMP_TZ, TIMESTAMP_LTZ, and DSINTERVAL, etc. Date format is used to describe which date format we used to store data. The template not able to change the interval representation of the database, but when we convert the character string into a date, at that time template determine how OLAP interprets the string.
Syntax:
date_format [specified date time format template]
Explanation
In the above syntax, we use date format command to specify which date format is used in the database. Here the specified data time format composes one or more date and time format for insert or display the records from the database.
How Date Format works in Oracle?
Now let’s see the date format is worked in oracle as follows.
Oracle uses a standard format for input and output that is DD – MON – YY and which is controlled by the NLS_DATE_FROMAT parameter.
When a user displays the date value at that time oracle first converts that date value into a specified internal format and this conversion is done by using the TO_CHAR function according to the date format. The default date format of oracle is that DD – MON – YY.
System date uses the following data function as follows.
- CURRENT_DATE: this function is used to display the current date in our system with current time zone.
- SYSDATE: it also used to return the current date of system.
- SYSTIMESTAMP: This function returns the current date and time of the system with fractional seconds.
General uses of TO_CHAR function as follows.
- MM: It is used to display month in numeric format for example: 05.
- MON: This format is used to display month with a name for example JAN.
- MONTH: By using this format we can display the full name of month for example JUN.
- DD: This is used to display the day of the month for example 21.
- DY: This format is used to display the name of the day for example SUN.
- YYYY: This four Y is used to display the year for example 2020.
- YY: By using this format we can display the last two digits of year for example 21.
Examples
Now let’s see a different example of date format as follows.
Suppose we need to see current system data. At that time we use the following statement as follows.
select sysdate from dual;
Explanation
In the above example, we use the select sysdate function to check system current date, here dual is a table name that is automatically created by oracle database with data directory and it is accessible to all users. The end output of the above query we illustrate by using the following snapshot.
The formats of date is depending on NLS_DATE_FORMAT parameter and suppose we need to display date and time in date – month – yy format at that time we can use the following statement as follows.
alter session set NLS_DATE_FORMAT='DD-MONTH-YY HH: MIpm';
Explanation
In the above statement, we use the alter session command to change the date format as per our requirement as shown in the above statement. The end output of the above query we illustrate by using the following snapshot.
Now we can check the date by using the following statement.
select sysdate from dual;
Suppose users need to display date and time in DD – MONTH – YYYY format at that time we can use the following statement as follows.
alter session set NLS_DATE_FORMAT=’DD-MONTH-YYYY HH:MIpm’;
Explanation
In the above statement, we use alter session command to change the date format as per our requirement as shown in above statement. The end output of the above query we illustrate by using the following snapshot.
Now we can check the date by using the following statement.
select sysdate from dual;
Example
Suppose users need to display date and time-fractional seconds at that time we can use the following statement as follows.
select systimestamp from dual;
Explanation
The end output of the above query we illustrate by using the following snapshot.
Example
We can translate date into a different format as per our requirement at that time we can TO_CHAR function with date format as follows.
Select to_char(sysdate,'DAY')"Todays_Day" FROM DUAL;
Explanation
In the above example, we use the TO_CHAR function to translate the date into a different format. The end output of the above query we illustrate by using the following snapshot.
Example
Suppose users need to display a date in Sunday, 8th January 2021 format so that we can use the following statement as follows.
select to_char(sysdate,'Day, ddth Month, yyyy')"Todays_Day" from dual;
Explanation
The end output of the above query we illustrate by using the following snapshot.
Example
Users need to see what was the day on 15 aug 2020 at that time we can use the following statement as follows.
select to_char(to_date('15-aug-2020','dd-mon-yyyy'),'Day')from dual;
Explanation
The end output of the above query we illustrate by using the following snapshot.
Example
Now suppose we need to see which day will occur 30 days from now at that time we can use the following statement as follows.
select sysdate+30 from dual;
Explanation
The end output of the above query we illustrate by using the following snapshot.
Rules and regulation for date format
- Format model is not able to change the internal representation of data value in the database. So we can use TO_CHAR format.
- Date format in oracle uses return value from database.
- Format of data value we can specify for oracle to store in the database.
Conclusion
We hope from this article you have understood about the Oracle date format. From this article, we have learned the basic syntax of date format and we also see different examples of date format. From this article, we learned how and when we use Oracle date format.
Recommended Articles
This is a guide to Oracle Date Format. Here we discuss the definition, How Date Format works in Oracle? examples with code implementation respectively. You may also have a look at the following articles to learn more –