sqloracle-database

Oracle Date returning 20xx instead of 19xx when format is mmddyy


I'm having issue while converting date format, I'm receiving as a text in the format mmddyy ( 031537 ), when I try to convert with to_date it is returning 2037 instead 1937

Appreciate if anyone has work around and I'm using oracle version 19c

select to_date('031537','mmddyy') from dual;
03/15/2037

select to_date('031537','mmddyrr') from dual;
03/15/2037

Tried to_date but no luck


Solution

  • In Oracle:


    The conclusion is that there is no 2-digit format model which would parse 37 and give you 1937 as the year.

    If you want to have a year of 1937 then pass a 4-digit year.

    SELECT TO_DATE('03151937','mmddyyyy') FROM DUAL;
    

    Alternatively, if you are always going to get a 2-digit year from the 20th century, split the string value and manually insert the century so you have a 4-digit year:

    WITH data (value) AS (
      SELECT '031537' FROM DUAL
    )
    SELECT TO_DATE(
             SUBSTR(value, 1, 4) || '19' || SUBSTR(value, 5),
             'MMDDYYYY'
           )
    FROM   data
    

    Both of which give the date: 1937-03-15 00:00:00

    fiddle