Monday, 11 June 2012

Difference Between DD-MON-RR and DD-MON-YY in Oracle SQL




SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 1998



SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-YY') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2098


DD-MON-RR
===========

If the specified two digits year is 0-49 and the two digits of the current 
year is 0-49 it returns the current century. 
SELECT TO_CHAR(TO_DATE('27-OCT-08', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2008

If the specified two digits year is 0-49 and the two digits of the current 
year is 50-99 it returns the century after the current one. 
ex: The Current year is 2058
SELECT TO_CHAR(TO_DATE('27-OCT-08', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2158

If the specified two digits year is 50-99 and the two digits of the current 
year is 0-49 it returns the century before the current one. 
SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 1998

If the specified two digits year is 50-99 and the two digits of the current 
year is 50-99 it returns the current century. 
ex: The Current year is 2058
SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2098

DD-MON-YY
===========
But in  all of the above scenario's   yy   will consider this century alone.



SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2098

SELECT TO_CHAR(TO_DATE('27-OCT-08', 'DD-MON-RR') ,'YYYY') "Year"
FROM DUAL;
The Output is : 2008




6 comments:

  1. Thank you for the excellent article. It has been very helpful.

    ReplyDelete
  2. These examples are very understandable.... and very clear....
    Thanq

    ReplyDelete
  3. Bullet 2. If the specified two digits year is 0-49 and the two digits of the current
    year is 50-99 it returns the century after the current one.
    ex: The Current year is 2058
    SELECT TO_CHAR(TO_DATE('27-OCT-08', 'DD-MON-RR') ,'YYYY') "Year"
    FROM DUAL;
    The Output is : 2158 ----> I was confused, why it won't return '2108'. Kindly suggest. Thanks in advance.

    ReplyDelete
    Replies
    1. It should be 2108. i also don't get this particular case. please explain with another example.

      Delete
  4. Say-
    Year 00-49 = L
    Year 50-99 = U

    1a. Current year = L and RR = L, RR Century = Current.
    1b. Current year = U and RR = U, RR Century = Current.

    2. Current year = L and RR = U, RR Century = Previous.
    3. Current year = U and RR = L, RR Century = Next.

    If both RR and the current year
    are in the same half of the century,
    RR is in the same century.

    If RR and and the current year
    are in the different half of the century,
    RR goes 1 century (+/-) twards the current year (U/L)

    Thanks.

    ReplyDelete
  5. In DD-MM-YY explanation part,

    There is should be DD-MON-YY instead of DD-MON-RR.

    Eg.

    SELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-YY') ,'YYYY') "Year"
    FROM DUAL;
    The Output is : 2098

    SELECT TO_CHAR(TO_DATE('27-OCT-08', 'DD-MON-YY') ,'YYYY') "Year"
    FROM DUAL;
    The Output is : 2008

    Please let me correct if i am wrong here.

    ReplyDelete