Lesson03 Part3 End
Lesson03 Part3 End
Lesson03 Part3 End
Customize Output
Lesson Agenda
3 - 20
3 - 21
0–49 50–99
If two digits The return date is in The return date is in
of the 0–49 the current century the century before
current the current one
year are: The return date is in The return date is in
50–99 the century after the current century
the current one
3 - 22
RR Date Format
The RR date format is similar to the YY element, but you can use it to specify different centuries. Use
the RR date format element instead of YY so that the century of the return value varies according to
the specified two-digit year and the last two digits of the current year. The table in the slide
summarizes the behavior of the RR element.
Current Year Given Date Interpreted (RR) Interpreted (YY)
1994 27-OCT-95 1995 1995
1994 27-OCT-17 2017 1917
2001 27-OCT-17 2017 2017
2048 27-OCT-52 1952 2052
2051 27-OCT-47 2147 2047
Note the values shown in the last two rows of the above table. As we approach the middle of the
century, then the RR behavior is probably not what you want.
SELECT sysdate
FROM dual;
;
3 - 24
3 - 25
3 - 26
3 - 27
Function Result
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
3 - 28
Date-Manipulation Functions
Date functions operate on Oracle dates. All date functions return a value of the DATE data type
except MONTHS_BETWEEN, which returns a numeric value.
• MONTHS_BETWEEN(date1, date2): Finds the number of months between date1 and
date2. The result can be positive or negative. If date1 is later than date2, the result is
positive; if date1 is earlier than date2, the result is negative. The noninteger part of the result
represents a portion of the month.
• ADD_MONTHS(date, n): Adds n number of calendar months to date. The value of n must
be an integer and can be negative.
• NEXT_DAY(date, 'char'): Finds the date of the next specified day of the week
('char') following date. The value of char may be a number representing a day or a
character string.
• LAST_DAY(date): Finds the date of the last day of the month that contains date
The above list is a subset of the available date functions. ROUND and TRUNC number functions can
also be used to manipulate the date values as shown below:
• ROUND(date[,'fmt']): Returns date rounded to the unit that is specified by the format
model fmt. If the format model fmt is omitted, date is rounded to the nearest day.
• TRUNC(date[, 'fmt']): Returns date with the time portion of the day truncated to the
unit that is specified by the format model fmt. If the format model fmt is omitted, date is
truncated to the nearest day.
The format models are covered in detail in the lesson titled “Using Conversion Functions and
Conditional Expressions.”
Oracle Database 11g: SQL Fundamentals I 3 - 28
Using Date Functions
Function Result
MONTHS_BETWEEN 19.6774194
('01-SEP-95','11-JAN-94')
ADD_MONTHS (‘31-JAN-96',1) '29-FEB-96'
NEXT_DAY ('01-SEP-95','FRIDAY') '08-SEP-95'
LAST_DAY ('01-FEB-95') '28-FEB-95'
3 - 29
Function Result
ROUND(SYSDATE,'MONTH') 01-AUG-03
ROUND(SYSDATE ,'YEAR') 01-JAN-04
TRUNC(SYSDATE ,'MONTH') 01-JUL-03
TRUNC(SYSDATE ,'YEAR') 01-JAN-03
3 - 30
3 - 32
Summary
Single-row functions can be nested to any level. Single-row functions can manipulate the following:
• Character data: LOWER, UPPER, INITCAP, CONCAT, SUBSTR, INSTR, LENGTH
• Number data: ROUND, TRUNC, MOD
• Date values: SYSDATE, MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY
Remember the following:
• Date values can also use arithmetic operators.
• ROUND and TRUNC functions can also be used with date values.
SYSDATE and DUAL
SYSDATE is a date function that returns the current date and time. It is customary to select
SYSDATE from a single-row public table called DUAL.