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.
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: TO_CHAR(col1,'format')
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:
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.)
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.
When adding or subtracting years and months, you get different behavior from
using ADD_MONTHS, which operates on values of type DATE, than from using
interval arithmetic on the timestamp types
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.
Oracle Date formats and date Arithmetic
A format model is a character literal that describes the format of datetime or numeric data stored in a character string. A format model does not change the internal representation of the value in the database. When you convert a character string into a date or number, a format model determines how Oracle Database interprets the string. In SQL statements, you can use a format model as an argument of the TO_CHAR and TO_DATE functions to specify:
The format for Oracle to use to return a value from the database
The format for a value you have specified for Oracle to store in the database
The datetime format model for the string '17:45:29' is 'HH24:MI:SS'.
The datetime format model for the string '11-Nov-1999' is 'DD-Mon-YYYY'.
The number format model for the string '$2,304.25' is '$9,999.99'.
The ADD_MONTHS function returns the date resulting from date plus integer months.
ADD_MONTHS has the parameters:
A datetime value or any value that can be converted to a datetime value.
An integer or any value that can be converted to an integer.
The return type is always DATE regardless of the data type of date. Supported data types are DATE, TIMESTAMP, ORA_TIMESTAMP and ORA_DATE.
Data types TIME, TT_TIME, TT_DATE and TT_TIMESTAMP are not supported.
If date is the last day of the month or if the resulting month has fewer days than the day component of date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as date.
Y2K and the Oracle date
By the time this course reaches you, it may be the year 2023. 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.