简体   繁体   中英

Why Year part of date in update query statement updating wrong year when year is >= 2050

I am trying to insert/update DATE_OF_RETIREMENT column, but when the year is >=2050, it takes as 1950.

Example:

UPDATE EMPLOYEE
SET STATUS             = '4',
    ISACTIVE           = 1,
    DATE_OF_RETIREMENT = '30-APR-49'
WHERE ID = 2001;
  

Here DATE_OF_RETIREMENT is updated with 30 APR 2049 .

But in following case:

UPDATE EMPLOYEE
SET STATUS             = '4',
    ISACTIVE           = 1,
    DATE_OF_RETIREMENT = '30-APR-52'
WHERE ID = 2001;
  

Here DATE_OF_RETIREMENT with updated with 30 APR 1952 .

Can anyone tell me the reason why this is happening?

Use 4 digits for years; I hope you aren't trying to save some disk space, are you? Y2K is 20 years behind us.

See the difference between RR and YY format mask for years. Depending on whether it (year) is before or after the 50th year in the century, you'll get a different result.

SQL> alter session set nls_date_format = 'dd.mm.yyyy';

Session altered.

SQL> select to_date('30-04-49', 'dd.mm-rr') val1_rr,
  2         to_date('30-04-49', 'dd.mm-yy') val2_yy,
  3         --
  4         to_date('30-04-52', 'dd.mm-rr') val3_rr,
  5         to_date('30-04-52', 'dd.mm-yy') val4_yy
  6  from dual;

VAL1_RR    VAL2_YY    VAL3_RR    VAL4_YY
---------- ---------- ---------- ----------
30.04.2049 30.04.2049 30.04.1952 30.04.2052

SQL>

The reason this is happening is because Oracle has a built-in century cutoff date, and the default is 50. Anything less than or equal to the cut-off year is considered to fall in the current century.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM