Sub topic 9
Two-digit years: RR versus YY
This chapter assumes you can already call TO_DATE and TO_CHAR with a format model, as covered earlier in this tutorial. It also assumes you know what DUAL is. The chapter covers how Oracle chooses a century for a two-digit year, where RR and YY differ, and how to avoid century mistakes.
Why two-digit years are a problem
A string such as 01-01-99 leaves out the century. TO_DATE must still return a full DATE, which always stores a four-digit year. The format element you choose decides which century Oracle fills in.
YYtakes the century from the current year.RRpicks the century closest to the current year, using a sliding 100-year window.RRRRaccepts four-digit input as written and applies theRRrules to two-digit input.YYYYuses the digits you give it and does not guess a century.
The examples in this chapter assume the current year is 2026. Oracle reads the current year from the database server's clock, so the same query can return different results in a later year.
The YY rule
YY always uses the first two digits of the current year. In 2026 the century prefix is 20, so every two-digit year lands between 2000 and 2099.
SELECT TO_CHAR(TO_DATE('01-01-99', 'DD-MM-YY'), 'YYYY') AS yy_99,
TO_CHAR(TO_DATE('01-01-05', 'DD-MM-YY'), 'YYYY') AS yy_05
FROM dual;This returns 2099 and 2005. For data written in the 1900s, YY gives wrong dates and raises no error.
The RR rule
RR looks at two things: the two-digit year in the string, and the last two digits of the current year. The table shows the result.
| Current year (last two digits) | Input year 00-49 | Input year 50-99 |
|---|---|---|
| 00-49 | Current century | Previous century |
| 50-99 | Next century | Current century |
In 2026 the current year falls in the 00-49 row. Input from 00 to 49 means 2000 to 2049. Input from 50 to 99 means 1950 to 1999.
SELECT TO_CHAR(TO_DATE('01-01-99', 'DD-MM-RR'), 'YYYY') AS rr_99,
TO_CHAR(TO_DATE('01-01-49', 'DD-MM-RR'), 'YYYY') AS rr_49,
TO_CHAR(TO_DATE('01-01-50', 'DD-MM-RR'), 'YYYY') AS rr_50,
TO_CHAR(TO_DATE('01-01-00', 'DD-MM-RR'), 'YYYY') AS rr_00
FROM dual;With a 2026 clock the result is 1999, 2049, 1950, 2000. The boundary sits between 49 and 50. Two consecutive input years, 49 and 50, land 99 years apart.
The window is centered on the current year. At any moment, RR produces years from roughly 50 years in the past to 49 years in the future.
Side by side
This query compares the two elements on the same inputs.
SELECT s AS input_text,
TO_CHAR(TO_DATE(s, 'DD-MM-RR'), 'YYYY') AS rr_year,
TO_CHAR(TO_DATE(s, 'DD-MM-YY'), 'YYYY') AS yy_year
FROM (SELECT '01-01-26' AS s FROM dual UNION ALL
SELECT '01-01-49' FROM dual UNION ALL
SELECT '01-01-50' FROM dual UNION ALL
SELECT '01-01-99' FROM dual)
ORDER BY s;In 2026 the rows are:
| input_text | rr_year | yy_year |
|---|---|---|
| 01-01-26 | 2026 | 2026 |
| 01-01-49 | 2049 | 2049 |
| 01-01-50 | 1950 | 2050 |
| 01-01-99 | 1999 | 2099 |
The two elements agree on 00-49 and disagree on 50-99. If your data has only recent dates, you will not see a difference until a value in the 50-99 range appears.
The default date format uses RR
In many installations the default NLS_DATE_FORMAT is DD-MON-RR. That default depends on the territory setting, so check your own session. When you rely on implicit conversion, for example inserting '15-MAR-99' into a DATE column, the session format decides the century.
SELECT value
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';If a session or an application changes the format to use YY, the same insert stores a different date.
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YY';
SELECT TO_CHAR(TO_DATE('15-MAR-99'), 'YYYY-MM-DD') AS parsed
FROM dual;Here TO_DATE has no format argument, so it falls back to the session format. The result is 2099-03-15. After ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-RR' the same query returns 1999-03-15. This is one more reason to pass an explicit format model, as discussed earlier in this tutorial.
RR in TO_CHAR
Parsing and formatting behave differently. In TO_CHAR, RR and YY both print the last two digits of the year. The century logic applies only when Oracle reads a string.
SELECT TO_CHAR(DATE '1999-06-30', 'RR') AS rr_out,
TO_CHAR(DATE '1999-06-30', 'YY') AS yy_out
FROM dual;Both columns return 99. A date printed with two digits cannot be read back reliably without knowing the rules in force at read time.
RRRR and YYYY with short input
RRRR is the safe choice when input can arrive with either two or four digits. It reads four digits as given and applies the RR window to two digits.
SELECT TO_CHAR(TO_DATE('01-01-99', 'DD-MM-RRRR'), 'YYYY') AS two_digit,
TO_CHAR(TO_DATE('01-01-2099', 'DD-MM-RRRR'), 'YYYY') AS four_digit
FROM dual;The result is 1999 and 2099.
YYYY with two-digit input does not guess a century. It reads the digits as a literal year, so '99' becomes year 0099. That date is valid, so no error appears, but it is almost never what you wanted. Check for it when a report shows dates in the first century.
The reverse mismatch does raise an error. If the string holds four digits and the model says RR or YY, Oracle stops reading after two digits and finds leftover input.
SELECT TO_DATE('01-01-1999', 'DD-MM-RR') FROM dual;This fails with ORA-01830: date format picture ends before converting entire input string. Use RRRR when you cannot be sure how many digits the input has.
Edge cases to know
- Boundary years move. In 2026 the cut-off between 2049 and 1950 is at input 49/50. When the current year reaches 2050, the table's other row applies. Input 00-49 will then mean 2100-2149, and 50-99 will mean 2050-2099. Code that parses stored two-digit strings is not stable across that change.
- Re-parsing old data. A string such as
'01-01-30'parsed in 2026 gives 2030. If the string records a 1930 date, the storedDATEis wrong and nothing flags it. Look for suspicious future dates after a load, for exampleWHERE hire_date > SYSDATE. - Testing. Tests that depend on
RRneed a known clock. Running the same test in a later year can change the result. - Calculations on the date.
RRis applied once, at parse time. Once the value is aDATE, it carries a full four-digit year and no later arithmetic depends onRR.
Choosing a strategy
- Prefer four-digit years in every interface, file, and format model.
- If you must accept two-digit years from an external source, use
RRfor dates near the present, and add a validity check against the range you expect. - If you know all values belong to one century, say so in the code. One approach is to append the century to the string before parsing.
SELECT TO_DATE('19' || '99-01-01', 'YYYY-MM-DD') AS fixed_century
FROM dual;- Use
YYonly when every value is meant to be in the current century and you want that fixed behavior.
Summary of rules
| Element | Reading `99` in 2026 | Reading `26` in 2026 | Reading a four-digit year |
|---|---|---|---|
| `YY` | 2099 | 2026 | Error (ORA-01830) |
| `RR` | 1999 | 2026 | Error (ORA-01830) |
| `RRRR` | 1999 | 2026 | Used as given |
| `YYYY` | 0099 | 0026 | Used as given |
Exercise
The current year is 2026. What years do TO_DATE('01-01-30', 'DD-MM-RR') and TO_DATE('01-01-30', 'DD-MM-YY') return, and what do they return for the input '01-01-80'?
Show answer
For '30', both return 2030, because the current year is in the 00-49 range and the input is also in the 00-49 range. For '80', RR returns 1980 (previous century) and YY returns 2080 (current century).
Exercise
A load script runs TO_DATE(birth_text, 'DD-MM-YYYY') on a column that sometimes holds '12-05-85'. No error appears, but a report shows some birth dates in the year 0085. Why does this happen, and how would you fix the script?
Show answer
YYYY does not apply a century rule. It reads 85 as year 0085, which is a valid date, so Oracle raises no error. Switching the model to DD-MM-RRRR reads two-digit input with the RR window (1985 in 2026) and still accepts four-digit input as given. If the source covers more than 50 years, ask the source to supply four-digit years instead.
Exercise
A query run in 2026 does TO_DATE('15-JUN-1998', 'DD-MON-RR'). What happens, and what is the fix?
Show answer
It fails with ORA-01830, because RR consumes only two digits and Oracle finds leftover characters in the input. Use RRRR or YYYY in the model, for example 'DD-MON-RRRR'.