0% found this document useful (0 votes)
91 views

PL - SQL Date and Time

PL_SQL Date and Time

Uploaded by

Ps Padhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

PL - SQL Date and Time

PL_SQL Date and Time

Uploaded by

Ps Padhu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

4/2/2019 PL/SQL Date and Time

PL/SQL - Date & Time


Advertisements

 Previous Page Next Page 

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

Field Values for Datetime and Interval Data Types


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.

Field Name Valid Datetime Values Valid Interval Values

YEAR -4712 to 9999 (excluding year 0) Any nonzero integer

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

DAY 01 to 31 (limited by the values of MONTH Any nonzero integer


and YEAR, according to the rules of the
calendar for the locale)

HOUR 00 to 23 0 to 23

MINUTE 00 to 59 0 to 59

00 to 59.9(n), where 9(n) is the


precision of time fractional seconds 0 to 59.9(n), where 9(n) is
SECOND the precision of interval
The 9(n) portion is not applicable for fractional seconds
DATE.

-12 to 14 (range accommodates


daylight savings time changes)
TIMEZONE_HOUR Not applicable
Not applicable for DATE or
TIMESTAMP.

00 to 59

TIMEZONE_MINUTE Not applicable for DATE or Not applicable


TIMESTAMP.

TIMEZONE_REGION Not applicable for DATE or TIMESTAMP. Not applicable

TIMEZONE_ABBR Not applicable for DATE or TIMESTAMP. Not applicable

The Datetime Data Types and Functions


Following are the Datetime data types −

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.

TIMESTAMP WITH TIME ZONE


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

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.

TIMESTAMP WITH LOCAL TIME ZONE


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) −

S.No Function Name & Description

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.

Timestamp functions (where, x has a timestamp value) −

S.No Function Name & Description

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.

EXTRACT({ YEAR | MONTH | DAY | HOUR | MINUTE | SECOND } | {


TIMEZONE_HOUR | TIMEZONE_MINUTE } | { TIMEZONE_REGION | }
TIMEZONE_ABBR ) FROM x)
2
Extracts and returns a year, month, day, hour, minute, second, or time zone
from x.

FROM_TZ(x, time_zone);

3 Converts the TIMESTAMP x and the time zone specified by time_zone to a


TIMESTAMP WITH TIMEZONE.

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);

6 Converts the TIMESTAMP WITH TIMEZONE x to a TIMESTAMP containing the date


and time in UTC.

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

SELECT SYSDATE FROM DUAL;

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

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

The Interval Data Types and Functions


Following are the Interval data types −

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 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);

Converts the string x to an INTERVAL DAY TO SECOND.


https://www.tutorialspoint.com/plsql/plsql_date_time.htm 5/6
4/2/2019 PL/SQL Date and Time

TO_YMINTERVAL(x);
4
Converts the string x to an INTERVAL YEAR TO MONTH.

 Previous Page Next Page 

Advertisements

Privacy Policy Cookies Policy Contact


© Copyright 2019. All Rights Reserved.

Enter email for newsletter go

https://www.tutorialspoint.com/plsql/plsql_date_time.htm 6/6

You might also like