PL/SQL - Date & 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 −
Datetime data types
Interval data types
The Datetime data types are −
DATE
TIMESTAMP
TIMESTAMP WITH TIME ZONE
TIMESTAMP WITH LOCAL TIME ZONE
The Interval data types are −
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
Both datetime and interval data types consist of fields. The values of these fields determine the value of the data type. The following table lists the fields and their possible values for datetimes and intervals.
00 to 59.9(n), where 9(n) is the precision of time fractional seconds
The 9(n) portion is not applicable for DATE.
-12 to 14 (range accommodates daylight savings time changes)
Not applicable for DATE or TIMESTAMP.
00 to 59
Not applicable for DATE or TIMESTAMP.
Following are the Datetime data types −
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 −
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.
It is a variant of TIMESTAMP that includes a time zone region name or a time zone offset in its value. The time zone offset is the difference (in hours and minutes) between local time and UTC. This data type is useful for collecting and evaluating date information across geographic regions.
It is another variant of TIMESTAMP that includes a time zone offset in its value.
Following table provides the Datetime functions (where, x has the datetime value) −
ADD_MONTHS(x, y);
Adds y months to x.
LAST_DAY(x);
Returns the last day of the month.
MONTHS_BETWEEN(x, y);
Returns the number of months between x and y.
NEXT_DAY(x, day);
Returns the datetime of the next day after x.
NEW_TIME;
Returns the time/day value from a time zone specified by the user.
ROUND(x [, unit]);
Rounds x.
SYSDATE();
Returns the current datetime.
TRUNC(x [, unit]);
Truncates x.
Timestamp functions (where, x has a timestamp value) −
CURRENT_TIMESTAMP();
Returns a TIMESTAMP WITH TIME ZONE containing the current session time along with the session time zone.
EXTRACT({ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | { TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | } TIMEZONE_ABBR ) FROM x)
Extracts and returns a year, month, day, hour, minute, second, or time zone from x.
FROM_TZ(x, time_zone);
Converts the TIMESTAMP x and the time zone specified by time_zone to a TIMESTAMP WITH TIMEZONE.
LOCALTIMESTAMP();
Returns a TIMESTAMP containing the local time in the session time zone.
SYSTIMESTAMP();
Returns a TIMESTAMP WITH TIME ZONE containing the current database time along with the database time zone.
SYS_EXTRACT_UTC(x);
Converts the TIMESTAMP WITH TIMEZONE x to a TIMESTAMP containing the date and time in UTC.
TO_TIMESTAMP(x, [format]);
Converts the string x to a TIMESTAMP.
TO_TIMESTAMP_TZ(x, [format]);
Converts the string x to a TIMESTAMP WITH TIMEZONE.
The following code snippets illustrate the use of the above functions −
Example 1
SELECT SYSDATE FROM DUAL; 
Output −
08/31/2012 5:25:34 PM 

Example 2
SELECT TO_CHAR(CURRENT_DATE, 'DD-MM-YYYY HH:MI:SS') FROM DUAL; 
Output −
31-08-2012 05:26:14

Example 3
SELECT ADD_MONTHS(SYSDATE, 5) FROM DUAL;
Output −
01/31/2013 5:26:31 PM 

Example 4
SELECT LOCALTIMESTAMP FROM DUAL; 
Output −
8/31/2012 5:26:55.347000 PM 

Following are the Interval data types −
IINTERVAL YEAR TO MONTH − It stores a period of time using the YEAR and MONTH datetime fields.
IINTERVAL YEAR TO MONTH − It stores a period of time using the YEAR and MONTH datetime fields.
INTERVAL DAY TO SECOND − It stores a period of time in terms of days, hours, minutes, and seconds. 
INTERVAL DAY TO SECOND − It stores a period of time in terms of days, hours, minutes, and seconds. 
NUMTODSINTERVAL(x, interval_unit);
Converts the number x to an INTERVAL DAY TO SECOND.
NUMTOYMINTERVAL(x, interval_unit);
Converts the number x to an INTERVAL YEAR TO MONTH.
TO_DSINTERVAL(x);
Converts the string x to an INTERVAL DAY TO SECOND.
TO_YMINTERVAL(x);
Converts the string x to an INTERVAL YEAR TO MONTH.
 Print
 Add Notes
 Bookmark this page