
In this post, we will see the difference between YYYY and RRRR in Oracle SQL. If not already knowing YYYY and RRRR are Date formatting predicates. Actually both of them impact the Year of the date to be presented to the calling program.
Theย RR
ย DateTime format element is similar to theย YYย DateTime format element, but it provides additional flexibility for storing date values in other centuries. Theย RRย DateTime format element lets you store 20th-century dates in the 21st century by specifying only the last two digits of the year.
When you use YYYY
ย in the date format code, it returns the current year as 4 digits.
While RRRR
ย format returns the 2-digit years in the rangeย 00
ย toย 49
ย are assumed to be in theย currentย century (ie have the same first two digits as the current year), and years given asย 50
ย throughย 99
ย are assumed to be in theย previousย century.
Statement from the docs on difference between YYYY and RRRR:
RR
Given a year with 2 digits, returns a year in the next century if the year is <50 and the last 2 digits of the current year are >=50; returns a year in the preceding century if the year is >=50 and the last 2 digits of the current year are <50.
RRRR
RRRR is the year rounded. Accepts either 4-digit or 2-digit input. If 2-digit provides the same return as RR. If you donโt want this functionality, simply enter the 4-digit year.
YYYY
YYYY is the 4 digit year and when SYYYY is passed with 4-digit year; โSโ prefixes BC dates with โ-โ.
YYY
YY
Y
Last 3, 2, or 1 digit(s) of year.
However, if you enter 35, both YY and RR will insert 2035. An important distinction.
Examples
SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
Year
----
1998
SELECT TO_CHAR(TO_DATE('27-OCT-21', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
Year
----
2021
select to_char(to_date('01-01-72','dd-mm-yyyy'),'yyyy') Year from dual;
Year
----
0072
select to_char(to_date('01-01-72','dd-mm-rrrr'),'yyyy') Year from dual;
Year
----
1972
Summary
In summary, there are 2 cases of the YEAR when the queries are issued :
CASE 1: If the specified two-digit year is 00 to 49, then
If the last two digits of the current year are 00 to 49, then the returned year has the same first two digits as the current year.
When the last two digits of the current year are 50 to 99, then the first 2 digits of the returned year are 1 greater than the first 2 digits of the current year.
CASE 2: If the specified two-digit year is 50 to 99, then
- If the last two digits of the current year are 00 to 49, then the first 2 digits of the returned year are 1 less than the first 2 digits of the current year.
- If the last two digits of the current year are 50 to 99, then the returned year has the same first two digits as the current year.
This oracle link gives some examples
Hope I am able to clear the Difference between yyyy and rrrr and when to use it in your pl SQL programming.
Do check out some PL SQL interview questions