| Lesson 6 || Date/time functions for date manipulation |
| Objective || Identify valid date-related functions and Oracle's standard date format. |
Oracle Date and Time Functions
This lesson looks at a few of the more commonly used date functions that work with the date portion of the date datatype. Since the date datatype also contains time, the next lesson illustrates functions you can use to manipulate the time portion of a date.
The default display format for dates in Oracle is: DD-MON-YY. However, dates are actually stored in the database as a large number that facilitates date comparisons and date arithmetic. There are many functions for manipulating dates. The following table lists them all.
| Function || Syntax || Description |
|Add n months to col1 |
|Date of the last day of the month for col1 |
|Number of months between two dates. Returns fraction of month if days of the two dates are different. |
NEW_TIME(col1, zone1, zone2)
|Converts date and time from zone1 to zone2 |
|Returns the date of the next day named by day (such as 'MONDAY') |
| Returns the date rounded to the nearest day by default; can round to nearest month, quarter, year, hour, and so on based on round_to format |
| Returns the date with the time removed; can truncate to nearest month, quarter, year, hour, and so on based on trunc_to format |
| Returns date and/or time based on format listed |
You can display an Oracle date as any of dozens of format combinations. Just use this basic layout:
The format parameter is always enclosed in single quotes and uses symbols defining how the date is displayed. Here are some of the date format symbols:
| Format parameter || Description |
| Month in numbers (01 - 12) |
| Month spelled out with initial capital letter (January - December) |
| Month spelled in all capital letters (JANUARY - DECEMBER) |
| Three character abbreviation of month in capital letters (JAN - DEC) |
| Day in numbers (01 - 31) |
| 01st, 02nd, and so on |
| number of day in the year (1 - 366) |
| Day spelled out with initial capital letter (Monday - Sunday) |
| Day of week in all capital letters (MONDAY - SUNDAY) |
| Last two digits of year (00 - 99) |
| Year spelled out with initial capital (such as Two Thousand) |
| Rounded year; if year is <50, it will round up to the next century if current year is > 50. |
HH or HH12
| Hour (1 - 12) |
| Hour (0 - 23) |
| Minutes (00 - 59) |
| Meridian in capital letters (AM or PM) |
| Meridian (A.M. or P.M.) |
| SS || Seconds (00 - 59) |
There are many more, but these are the major ones.
The following Slide Show shows several examples of the
TO_CHAR() function with a variety of date formats, plus some date arithmetic.
- This example shows the day and month spelled out and the year displayed as a four-digit year.
- You can add or subtract days to a date with a plus sign or minus sign
- You can find the difference between two dates by subtracting them. When the difference is calculated, the time stored with each date is taken into account.
- You can also use the functions, such as NEXT_DAY, to calculate dates.
Date Formats Date Arithmetic
Y2K and the Oracle date
By the time this course reaches you, it may be the year 2013.
The Oracle date
has always contained all four digits of the year.
Even when your input only requires two digits entered for the year, Oracle adds in the century before storing the date in the database.
In the next lesson, you will learn how to identify valid time-related date/time functions.