PL - SQL Date and Time
PL - SQL Date and Time
In this chapter, we will discuss the Date and Time in PL/SQL. There are two classes of date
and time related data types in PL/SQL −
DATE
TIMESTAMP
MONTH 01 to 12 0 to 11
https://www.tutorialspoint.com/plsql/plsql_date_time.htm 1/6
4/2/2019 PL/SQL Date and Time
HOUR 00 to 23 0 to 23
MINUTE 00 to 59 0 to 59
00 to 59
DATE
It stores date and time information in both character and number datatypes. It is made of
information on century, year, month, date, hour, minute, and second. It is specified as −
TIMESTAMP
It is an extension of the DATE data type. It stores the year, month, and day of the DATE
datatype, along with hour, minute, and second values. It is useful for storing precise time
values.
https://www.tutorialspoint.com/plsql/plsql_date_time.htm 2/6
4/2/2019 PL/SQL Date and Time
time and UTC. This data type is useful for collecting and evaluating date information across
geographic regions.
Following table provides the Datetime functions (where, x has the datetime value) −
ADD_MONTHS(x, y);
1
Adds y months to x.
LAST_DAY(x);
2
Returns the last day of the month.
MONTHS_BETWEEN(x, y);
3
Returns the number of months between x and y.
NEXT_DAY(x, day);
4
Returns the datetime of the next day after x.
NEW_TIME;
5
Returns the time/day value from a time zone specified by the user.
ROUND(x [, unit]);
6
Rounds x.
SYSDATE();
7
Returns the current datetime.
TRUNC(x [, unit]);
8
Truncates x.
1 CURRENT_TIMESTAMP();
https://www.tutorialspoint.com/plsql/plsql_date_time.htm 3/6
4/2/2019 PL/SQL Date and Time
Returns a TIMESTAMP WITH TIME ZONE containing the current session time
along with the session time zone.
FROM_TZ(x, time_zone);
LOCALTIMESTAMP();
4
Returns a TIMESTAMP containing the local time in the session time zone.
SYSTIMESTAMP();
5 Returns a TIMESTAMP WITH TIME ZONE containing the current database time
along with the database time zone.
SYS_EXTRACT_UTC(x);
TO_TIMESTAMP(x, [format]);
7
Converts the string x to a TIMESTAMP.
TO_TIMESTAMP_TZ(x, [format]);
8
Converts the string x to a TIMESTAMP WITH TIMEZONE.
Examples
The following code snippets illustrate the use of the above functions −
Example 1
Output −
https://www.tutorialspoint.com/plsql/plsql_date_time.htm 4/6
4/2/2019 PL/SQL Date and Time
08/31/2012 5:25:34 PM
Example 2
Output −
31-08-2012 05:26:14
Example 3
Output −
01/31/2013 5:26:31 PM
Example 4
Output −
8/31/2012 5:26:55.347000 PM
IINTERVAL YEAR TO MONTH − It stores a period of time using the YEAR and
MONTH datetime fields.
Interval Functions
S.No Function Name & Description
NUMTODSINTERVAL(x, interval_unit);
1
Converts the number x to an INTERVAL DAY TO SECOND.
NUMTOYMINTERVAL(x, interval_unit);
2
Converts the number x to an INTERVAL YEAR TO MONTH.
3 TO_DSINTERVAL(x);
TO_YMINTERVAL(x);
4
Converts the string x to an INTERVAL YEAR TO MONTH.
Advertisements
https://www.tutorialspoint.com/plsql/plsql_date_time.htm 6/6