Chapter 6 PLSQL
Chapter 6 PLSQL
The year, month or day components of a DATE data type can be found using the EXTRACT( [ YEAR |
MONTH | DAY
] FROM datevalue )
FROM DUAL;
Outputs:
2016 7 25
Using CAST( datevalue AS TIMESTAMP ) to convert the DATE to a TIMESTAMP and then using EXTRACT( [
For example:
FROM (
);
Outputs:
9 42 1
Section 6.5: Generating Dates with No Time Component
All DATEs have a time component; however, it is customary to store dates which do not need to include
time
(More information on the date format models can be found in the Oracle documentation.)
or:
SELECT TO_DATE(
'NLS_DATE_LANGUAGE = American'
FROM DUAL;
(If you are converting language specific terms such as month names then it is good practice to include
the 3rd nlsparam
date_value DATE
);
Oracle will implicitly cast a TIMESTAMP to a DATE when storing it in a DATE column of a table; however
you can
In Oracle a DATE data type does not have a format; when Oracle sends a DATE to the client program
(SQL/Plus,
SQL/Developer, Toad, Java, Python, etc) it will send 7- or 8- bytes which represent the date.
A DATE which is not stored in a table (i.e. generated by SYSDATE and having "type 13" when using the
DUMP()
command) has 8-bytes and has the structure (the numbers on the right are the internal representation
of
2012-11-26 16:41:09):
3 Month 11
4 Day 26
5 Hours 16
6 Minutes 41
7 Seconds 9
8 Unused 0
A DATE which is stored in a table ("type 12" when using the DUMP() command) has 7-bytes and has the
structure (the
3 Month 11
4 Day 26
5 Hours + 1 17
6 Minutes + 1 42
7 Seconds + 1 10
If you want the date to have a specific format then you will need to convert it to something that has a
format (i.e. a
string). The SQL client may implicitly do this or you can explicitly convert the value to a string using
TO_CHAR( DATE,
format_model, nls_params ).
(Note: if a format model is not provided then the NLS_DATE_FORMAT session parameter will be used as
the default format
model; this can be different for every session so should not be relied on. It is good practice to always
specify the format
model.)
date_value DATE
);
Then:
Outputs:
FORMATTED_DATE
--------------
2000-01-01
2016-07-21
2016-07-21
And:
SELECT TO_CHAR(
date_value,
'NLS_DATE_LANGUAGE = French'
) AS formatted_date
FROM table_name;
Outputs:
FORMATTED_DATE
-----------------------------
Dates
When SQL/Plus or SQL Developer display dates they will perform an implicit conversion to a string using
the default
date format model (see the Setting the Default Date Format Model example).
You can change how a date is displayed by changing the NLS_DATE_FORMAT parameter.
The DATE data type does not handle time zones or changes in daylight savings time.
Either:
A DATE can be stored as Coordinated Universal Time (UTC) and converted to the current session time
zone like this:
SELECT FROM_TZ(
CAST(
),
'UTC'
AT LOCAL AS TIME
FROM DUAL;
If you run ALTER SESSION SET TIME_ZONE = '+01:00'; then the output is:
TIME
------------------------------------
and ALTER SESSION SET TIME_ZONE = 'PST'; then the output is:
TIME
------------------------------------
Oracle does not handle leap seconds. See My Oracle Support note 2019397.2 and 730795.1 for more
details.
Outputs 5
ALTER SESSION SET NLS_TERRITORY = 'UNITED KINGDOM'; -- First day of week is Monday
Outputs 4
To do this independent of the NLS settings, you can truncate the date to midnight of the current day (to
remove any
fractions of days) and subtract the date truncated to the start of the current iso-week (which always
starts on
Monday):
When Oracle implicitly converts from a DATE to a string or vice-versa (or when TO_CHAR() or TO_DATE()
are explicitly
called without a format model) the NLS_DATE_FORMAT session parameter will be used as the format
model in the
conversion. If the literal does not match the format model then an exception will be raised.
You can set this value within your current session using:
(Note: this does not change the value for any other users.)
If you rely on the NLS_DATE_FORMAT to provide the format mask in TO_DATE() or TO_CHAR() then you
should not be
Months or Years
The difference in months between two dates can be found using the MONTHS_BETWEEN( date1, date2
):
Outputs:
DIFFERENCE
----------
12
If the difference includes part months then it will return the fraction of the month based on there being
31 days in
each month:
SELECT MONTHS_BETWEEN( DATE '2015-02-15', DATE '2015-01-01' ) AS difference FROM DUAL;
Outputs:
DIFFERENCE
----------
1.4516129
Due to MONTHS_BETWEEN assuming 31 days per month when there can be fewer days per month then
this can result
Example:
FROM DUAL;
Output:
The difference in years can be found by dividing the month difference by 12.