Date Functions
Date Functions
If you store date and time information in Oracle, you have two different
options for the column's datatype - DATE and TIMESTAMP.
DATE is the datatype that we are all familiar with when we think about
representing date and time values. It has the ability to store the month, day,
year, century, hours, minutes, and seconds. It is typically good for
representing data for when something has happened or should happen in the
future. The problem with the DATE datatype is its' granularity when
trying to determine a time interval between two events when the events
happen within a second of each other. This issue is solved with the
TIMESTAMP datatype.
In order to represent the date stored in a more readable format, the TO_CHAR
function has traditionally been wrapped around the date:
SELECT TO_CHAR(hiredate,'DD.MM.YYYY:HH24:MI:SS') "hiredate"
FROM emp;
hiredate
17.12.1980:00:00:00
20.02.1981:00:00:00
...
14 rows selected.
1
Working with Dates
• Oracle stores dates in an internal numeric format representing the
century, year, month, day, hours, minutes, seconds.
• The default date format is DD-MON-YY.
• SYSDATE is a function returning date and time.
• DUAL is a dummy table used to view SYSDATE.
Oracle Date Format
The default display and input format for any date is DD-MON-YY. Valid
Oracle dates are behween Januar 1, 4712 B C. , and December 31, 9994 A.D.
SYSDATE
SYSDATE is a date function that returns the current date and time. You can
use SYSDATE just as you would use any other column name. For example,
you can display the current date by selecting SYSDATE from a table. It is
customary to select SYSDATE from a dummy table called DUAL .
DUAL
The DUAL table is owned by the user SYS and can be accessed by users. It
contains one column, DUMMY, and one row with the value X. The DUAL
table is useful when you want to return a value once only — for instance, the
value of a constant, pseudocolumn, or expression that is not derived from a
table with user data.
Example
SELECT sysdate
FROM dual;
SYSDATE
18/03/2007
2
Date Functions
The Built-In Date Functions
+ame Description
LAST_DAY Returns the last day in the month of the specified date.
NEW_TIME Returns the date/time value, with the time shifted as requested by the
specified time zones.
NEXT_DAY Returns the date of the first weekday specified that is later than the
date.
SYSDATE Returns the current date and time in the Oracle Server.
TRUNC Truncates the specified date of its time portion according to the
format unit provided.
3
Arithmetic with Dates
4
Addition and Subtraction of Dates
You can add and subtract number constants as well as other dates from dates.
Oracle interprets number constants in arithmetic date expressions as numbers of
days. For example:
• SYSDATE + 1 is tomorrow
• SYSDATE - 7 is one week ago
• SYSDATE + (10/1440) is ten minutes from now.
Subtracting the HIREDATE column of the EMP table from SYSDATE returns
the number of days since each employee was hired.
SELECT '03.12.2004:10:34:24' "Now",
TO_CHAR(hiredate,'DD.MM.YYYY:HH24:MI:SS') "Hiredate",
TO_DATE('03.12.2004:10:34:24','DD.MM.YYYY:HH24:MI:SS')
- hiredate "Hired since [Days]"
FROM emp;
Note:
You cannot multiply or divide DATE values. Oracle provides functions
for many common date operations.
SELECT '13.02.2007:10:34:24' ".imdi",
TO_DATE('13.02.2007:10:34:24','DD.MM.YYYY:HH24:MI:SS')
- TO_DATE( '28/11/1942:10:17:36' , 'DD/MM/YYYY:HH24:MI:SS' )
FROM dual;
TO_DATE('13.02.2007:10:34:24','DD.MM.YYYY:HH24:MI:SS')-
Şimdi
TO_DATE('28/11/1942:10:17:36','DD/MM/YYYY:HH24:MI:SS')
13.02.2007:10:34:24 23453,0117
5
Using Arithmetic Operators with Dates
SELECT ename, (SYSDATE - hiredate) / 7 WEEKS
FROM emp
WHERE deptno = 10;
E+AME WEEKS
CLARK 1344,86479
KING 1321,86479
MILLER 1312,29336
6
Using Date Functions
MONTHS_BETWEEN ('01-SEP-95', '11–JAN–94‘) 19.6774194
LAST_DAY('01-SEP-95') ’30-SEP-95’
7
Date Functions (continued)
For all employees employed for fewer than 200 months, display the
employee number, hiredate, number of months employed, six-month review
date, fırst Friday after hiredate, and last day of the month when hired.
SELECT empno, hiredate,
MONTHS_BETWEEN(SYSDATE, hiredate) TENURE,
ADD_MONTHS(hiredate, 6) REVIEW,
NEXT_DAY(hiredate, 'CUMA') CUMA ,
LAST_DAY(hiredate) Giriş
FROM emp
WHERE MONTHS_BETWEEN (SYSDATE, hiredate) > 310;
8
ADD_MONTHS
9
ADD_MONTHS
ADD_MONTHS always shifts the date by whole months. You can provide
a fractional value for the month_shift parameter, but ADD_MONTHS will
always round down to the whole number nearest zero, as shown in these
examples:
10
The LAST_DAY function
The LAST_DAY function returns the date of the last day of the month for a
given date. The specification is:
FUNCTION LAST_DAY (date_in IN DATE) RETURN DATE
This function is useful because the number of days in a month varies
throughout the year. With LAST_DAY, for example, you do not have to try to
figure out if February of this or that year has 28 or 29 days. Just let
LAST_DAY figure it out for you.
Here are some examples of LAST_DAY:
• Go to the last day in the month:
LAST_DAY ('12-JAN-99') ==> 31-JAN-1999
• If already on the last day, just stay on that day:
LAST_DAY ('31-JAN-99') ==> 31-JAN-1999
• Get the last day of the month three months after being hired:
LAST_DAY (ADD_MONTHS (hiredate, 3))
• Tell me the number of days until the end of the month:
LAST_DAY (SYSDATE) - SYSDATE
11
LAST_DAY (date)
LAST_DAY returns the date of the last day of the month that contains date.
The return type is always DATE, regardless of the datatype of date.
Example
The following statement determines how many days are left in the current
month:
SELECT SYSDATE,
LAST_DAY(SYSDATE) "Last",
LAST_DAY(SYSDATE) - SYSDATE "Days Left"
FROM DUAL;
LAST_DAY(T
28/02/2007
12
+EXT_DAY (date, day)
NEXT_DAY returns the date of the first weekday named by day that is later
than date. The return type is always DATE, regardless of the datatype of date.
The argument day must be a day of the week in the date language of your
session, either the full name or the abbreviation. The minimum number of
letters required is the number of letters in the abbreviated version. Any
characters immediately following the valid abbreviation are ignored. The
return value has the same hours, minutes, and seconds component as the
argument date.
Example
Return the date of the next Monday after now:
SELECT TO_CHAR ( NEXT_DAY (sysdate, 'PAZARTESĐ' ) ,
'DD.MM.YYYY' )
"Next Monday from now"
FROM DUAL;
13
MO+THS_BETWEE+ function
The MONTHS_BETWEEN function calculates the number of months
between two dates and returns that difference as a number. The specification
is:
MONTHS_BETWEEN (date1, date2)
14
MO+THS_BETWEE+ (continued)
Here are some examples of the uses of MONTHS_BETWEEN:
• Calculate two ends of month, the first earlier than the second:
MONTHS_BETWEEN ('31-JAN-1994', '28-FEB-1994')
==> -1
• Calculate two ends of month, the first later than the second:
MONTHS_BETWEEN ('31-MAR-1995', '28-FEB-1994')
==> 13
• Calculate when both dates fall in the same month:
MONTHS_BETWEEN ('28-FEB-1994', '15-FEB-1994')
==> 0
• Perform months_between calculations with a fractional component:
• MONTHS_BETWEEN ('31-JAN-1994', '1-MAR-1994')
==> -1.0322581
• MONTHS_BETWEEN ('31-JAN-1994', '2-MAR-1994')
==> -1.0645161
MONTHS_BETWEEN ('31-JAN-1994', '10-MAR-1994')
==> -1.3225806
15
The ROUND function
The ROUND function rounds a date value to the nearest date as specified by a
format mask. It is just like the standard numeric ROUND function, which
rounds a number to the nearest number of specified precision, except that it
works with dates. The specification for ROUND is as follows:
ROUND (date [, format_mask VARCHAR2])
It returns a date.
The ROUND function always rounds the time component of a date to
midnight (12:00 A.M.). The format mask is optional. If you do not include a
format mask, ROUND rounds the date to the nearest day. In other words, it
checks the time component of the date. If the time is past noon, then ROUND
returns the next day with a time component of midnight.
Examples
Round up to the next century:
TO_CHAR (ROUND (TO_DATE ('01-MAR-1994'), 'CC'),
'DD-MON-YYYY')
01-JAN-2000
16
ROUND (TO_DATE ('15-APR-1994'), 'Q')
==> 01-APR-1994
17
In the rest of the examples I use TO_DATE in order to pass a time
component to the ROUND function, and TO_CHAR to display the new
time.
Round back to nearest day (time always midnight):
TO_CHAR (ROUND (TO_DATE ('11-SEP-1994 10:00 AM',
'DD-MON-YY HH:MI AM'), 'DD'),
'DD-MON-YY HH:MI AM')
11-SEP-1994 12:00 AM
18
The TRUNC function
The TRUNC function truncates date values according to the specified format
mask. The specification for TRUNC is:
TRUNC (date [, format_mask VARCHAR2])
It returns a date.
The TRUNC date function is similar to the numeric FLOOR function.
Here are some examples of TRUNC for dates (all assuming a default date format mask of
DD-MON-YYYY):
Without a format mask, TRUNC sets the time to 12:00 A.M. of the same day:
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 9:36 AM', 'DD-
MON-YYYY HH:MI AM'))
11-SEP-1994 12:00 AM
19
TRUNC (TO_DATE ('15-APR-1994'), 'Q')
==> 01-APR-1994
In the rest of the examples I use TO_DATE to pass a time component to the
TRUNC function, and TO_CHAR to display the new time:
Trunc back to the beginning of the current day (time is always midnight):
TO_CHAR (TRUNC (TO_DATE ('11-SEP-1994 10:00 AM',
'DD-MON-YYYY HH:MI AM'), 'DD'),
'DD-MON-YYYY HH:MI AM')
==> 11-SEP-1994 12:00 AM
20
New_Time Function
In Oracle/PLSQL, the new_time function returns a date in time zone1 to a
date in time zone2.
The syntax for the new_time function is:
new_time( date, zone1, zone2 )
zone1 and zone2 can be any of the following values:
Value Description
AST Atlantic Standard Time
ADT Atlantic Daylight Time
BST Bering Standard Time
BDT Bering Daylight Time
CST Central Standard Time
CDT Central Daylight Time
EST Eastern Standard Time
EDT Eastern Daylight Time
GMT Greenwich Mean Time
HST Alaska-Hawaii Standard Time
HDT Alaska-Hawaii Daylight Time
MST Mountain Standard Time
MDT Mountain Daylight Time
NST Newfoundland Standard Time
PST Pacific Standard Time
PDT Pacific Daylight Time
YST Yukon Standard Time
YDT Yukon Daylight Time
21
+EW_TIME FU+CTIO+
+EW_TIME('
16/03/2007
+EW_TIME(T
31/10/2003
22