You are on page 1of 12

Date Functions

Copyright © 2007, Oracle. All rights reserved.


Date Functions

What Will I Learn?


In this lesson, you will learn to:
• Select and apply the single-row functions
MONTHS_BETWEEN, ADD_MONTHS,
NEXT_DAY, LAST_DAY, ROUND, and
TRUNC that operate on date data
• Explain how date functions transform Oracle
dates into date data or a numeric value
• Demonstrate proper use of the arithmetic
operators with dates
• Demonstrate the use of SYSDATE and date
functions
• State the implications for world businesses to
be able to easily manipulate data stored in
date format

Copyright © 2007, Oracle. All rights reserved. 2


Date Functions

Why Learn It?


Have you ever wondered how many days
remain in the school year or how many weeks
there are until graduation? Because the Oracle
database stores dates as numbers, it's easy to
perform calculations on dates using addition
and subtraction.

Businesses depend on being able to use date


functions to schedule payrolls and payments,
track employee performance reviews and years
of service, or keep track of orders and
shipments. All of these business needs are
easily handled using simple SQL date
functions.

Copyright © 2007, Oracle. All rights reserved. 3


Date Functions

Tell Me / Show Me
DATE

The default display format for dates is


DD-MON-RR -- that is, 02-DEC-99.

However, the Oracle database stores dates


internally with a numeric format, representing
the century, year, month, day, hours, minutes,
and seconds.

The default display and input format for any date


is DD-MON-RR. Valid Oracle dates are between
January 1, 4712 B.C., and December 31, 9999
A.D. This represents the range of dates that you
can store successfully in an Oracle database.
Copyright © 2007, Oracle. All rights reserved. 4
Date Functions

Tell Me / Show Me
When a record with a date column is inserted
into a table, the century information is picked up
from the SYSDATE function. SYSDATE is a date
function that returns the current database server
date and time. sdp_s01_l03_a01

SYSDATE

To display the current date, use the DUAL table.


SELECT SYSDATE
FROM DUAL;

Copyright © 2007, Oracle. All rights reserved. 5


Date Functions

Tell Me / Show Me
The DATE data type always stores year
information as a four-digit number internally: two
digits for the century and two digits for the year.
For example, the Oracle database stores the year
as 1996 or 2004, not just as 96 or 04. sdp_s01_l03_a01

Although the internal storage keeps track of the


complete date, when the date column is
displayed on the screen, the century component
is not displayed by default.

Copyright © 2007, Oracle. All rights reserved. 6


Date Functions

Tell Me / Show Me Date Functions

Function Description
The date functions shown in the MONTHS_BETWEEN Number of months
between two dates
table operate on Oracle dates. All of
ADD_MONTHS Add calendar months to
the date functions return a value date
with a DATE data type except the NEXT_DAY Next day of the date
MONTHS_BETWEEN function, specified
which returns a numeric data type LAST_DAY Last day of the month
value. ROUND Round date
TRUNC Truncate date

Copyright © 2007, Oracle. All rights reserved. 7


Date Functions

Tell Me / Show Me Date Functions

Function Description
MONTHS_BETWEEN Number of months
between two dates
ADD_MONTHS Add calendar months to
date

The following query shows how the NEXT_DAY Next day of the date
specified
date functions are used. LAST_DAY Last day of the month
ROUND Round date
TRUNC Truncate date

SELECT employee_id, hire_date,


ROUND MONTHS_BETWEEN(SYSDATE, hire_date) AS TENURE,
ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'),
LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36;

Copyright © 2007, Oracle. All rights reserved. 8


Date Functions

Tell Me / Show Me
SELECT employee_id, hire_date,
ROUND MONTHS_BETWEEN(SYSDATE, hire_date) AS
TENURE,
ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'),
LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) >
36;

The result set from this query returns 20 rows


including:

EMPLOYEE_ID HIRE_DATE TENURE REVIEW NEXT_DAY LAST_DAY


101 21-SEP-89 181 21-MAR-90 22-SEP-89 30-SEP-89

Copyright © 2007, Oracle. All rights reserved. 9


Date Functions

Tell Me / Show Me
Terminology
Key terms used in this lesson include:

ADD_MONTHS
LAST_DAY
MONTHS_BETWEEN
NEXT_DAY
SYSDATE

Copyright © 2007, Oracle. All rights reserved. 10


Date Functions

Summary
In this lesson you have learned to:

• Select and apply the single-row functions


MONTHS_BETWEEN, ADD_MONTHS,
NEXT_DAY, LAST_DAY, ROUND, and TRUNC
that operate on date data
• Explain how date functions transform Oracle
dates into date data or a numeric value
• Demonstrate proper use of the arithmetic
operators with dates
• Demonstrate the use of SYSDATE and date
functions
• State the implications for world businesses to be
able to easily manipulate data stored in date
format
Copyright © 2007, Oracle. All rights reserved. 11
Date Functions

Summary
Practice Guide

The link for the lesson practice guide can be


found in the course outline.

Copyright © 2007, Oracle. All rights reserved. 12