简体   繁体   English

为什么更新查询语句中日期的年份部分在年份> = 2050时更新错误年份

[英]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.我正在尝试插入/更新DATE_OF_RETIREMENT列,但是当年份 >=2050 时,它需要 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 .这里DATE_OF_RETIREMENT更新为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 .这里DATE_OF_RETIREMENT更新为30 APR 1952

Can anyone tell me the reason why this is happening?谁能告诉我发生这种情况的原因?

Use 4 digits for years;使用 4 位数字表示年份; I hope you aren't trying to save some disk space, are you?我希望你不是想节省一些磁盘空间,是吗? Y2K is 20 years behind us. Y2K 落后了我们 20 年。

See the difference between RR and YY format mask for years.多年来查看RRYY格式掩码之间的区别。 Depending on whether it (year) is before or after the 50th year in the century, you'll get a different result.根据它(年份)是在本世纪第 50 年之前还是之后,您会得到不同的结果。

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.发生这种情况的原因是因为 Oracle 具有内置的世纪截止日期,默认值为 50。小于或等于截止年份的任何内容都被视为当前世纪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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