两个日期之间的差异
我有一个表,其中包含以下数据
fromDate | toDate
20JAN11 | 29DEC30
两个日期都是 21 世纪的(即 2011 年和 2030 年),但只存储最后两个字符。
为什么针对上述数据的以下语句(当从 PL/SQL 模块内运行时)总是返回正值
dateDifference := (fromDate - toDate)
如果我从 sqlplus 运行以下语句,我会得到正确的负值。
select to_date('20JAN11','DDMONYY')-to_Date('29DEC30','DDMONYY') from dual;
我记得在某处读到Oracle有时会使用错误的世纪,但我不太记得发生这种情况的确切场景。
I have a table that has the following data
fromDate | toDate
20JAN11 | 29DEC30
Both dates are for the 21st Century (i.e. 2011 and 2030) but only the last two characters are stored.
Why is the following statement (when run from within a PL/SQL module) against the above data always returns a positive value
dateDifference := (fromDate - toDate)
If i run the following statement from sqlplus i get the correct negative value which is correct.
select to_date('20JAN11','DDMONYY')-to_Date('29DEC30','DDMONYY') from dual;
I remember reading somewhere that Oracle would sometimes use the wrong century but i dont quite remember the exact scenario where that would happen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设这些列是 DATE 数据类型,情况似乎是这样:Oracle 始终以包含全年的内部格式存储 DATE 值。您只看到 2 位数年份的事实与用于将日期转换为字符串以进行显示的日期格式有关。因此,存储的世纪值很可能不是您想象的那样。
尝试选择具有明确格式的日期以查看您真正存储的内容:
Assuming those columns are of
DATE
datatype, which seems to be the case: Oracle always storesDATE
values in an internal format which includes the full year. The fact that you are seeing only a 2-digit year has to do with the date format used to convert the date to a string for display. So most likely the stored century values are not what you think they are.Try selecting the dates with an explicit format to see what you really have stored:
似乎在我的 10g 数据库上无论哪种方式都对我有用:
编辑:也适用于 YY 而不是 RR 年份格式。
EDIT2:你的意思是这样的?
正如@Alex 所说,您可能想验证您的数据。
Seems to work for me either way on my 10g database:
EDIT: works for YY instead of RR year format as well.
EDIT2: Something like this, you mean?
As @Alex states, you may want to verify your data.
无需格式化也
可以尝试在表上运行第一个查询:
看看年份是否是您实际期望的。
(编辑:更改为使用两位数年份)
works without formatting as well
try running the first query on your table:
see if the years are what you actually expect.
(Edit: changed to use two digit years)