根据 SYSDATE 获取列值
我有一个表,其中有 2 列。定义是
CREATE TABLE LOGGING_T
(
TSTAMP DATE,
LINE VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/
TSTAMP 列具有诸如 30-NOV-11
、29-NOV-11
... 等值。现在我正在执行此查询
select * from LOGGING_T where TSTAMP >= (SYSDATE - 1)
当前系统日期是01-DEC-11
。理想情况下,上述语句应该返回 TSTAMP = 30-NOV-11
的记录,因为我正在执行 SYSDATE-1
,这将是 30-NOV-11< /代码>。但它没有获取这些记录。为什么?
但是,如果我执行此查询
select * from LOGGING_T where TSTAMP >= (SYSDATE - 2)
,那么它会获取 TSTAMP
为 30-NOV-11
的记录。我在这个简单的日期操作中做错了什么吗?
I have a table wchih has 2 columns. The definition is
CREATE TABLE LOGGING_T
(
TSTAMP DATE,
LINE VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/
The colulmn TSTAMP has values like 30-NOV-11
, 29-NOV-11
... and so on. Now i am doing this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 1)
The current system date is 01-DEC-11
. Ideally, the above statement should return records which has TSTAMP = 30-NOV-11
since i am doing SYSDATE-1
which would be 30-NOV-11
. But it isn't fetching those records. Why?
However, if i do this query
select * from LOGGING_T where TSTAMP >= (SYSDATE - 2)
Then it fetches records who TSTAMP
is 30-NOV-11
. Am i doing something wrong in this simple date operation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DATE
包含一天中的时间和日期。如果
SYSDATE
为2011-12-01 1:18:00 PM
,则SYSDATE-1
将为2011-11-30 1 :18:00 下午。
您期望从 11 月 30 日开始查找的行是在时间元素之前还是之后?
如果您不关心时间,只想根据日期进行过滤,则可以使用
TRUNC()
:您可能想也可能不想确定比较的双方运算符被
TRUNC()
编辑,因为TRUNC()
只会强制日期的时间元素为午夜。A
DATE
contains time of day as well as the date.If
SYSDATE
was2011-12-01 1:18:00 PM
thenSYSDATE-1
would be2011-11-30 1:18:00 PM
.Are the rows you are expecting to find from November 30th before or after the time element?
If you don't care about the time, and only want to filter based on the date, you can use
TRUNC()
:You'll may or may not want to make sure both sides of your comparison operator are
TRUNC()
ed becauseTRUNC()
will just force the time element of the date to be midnight.值 SYSDATE 也包含时间部分。数据库中的日期很可能也包含时间部分。
将查询更改为 :
以查看从昨天 00:00 开始记录的所有记录。
要查看实际的时间分量,请使用 char。
The value SYSDATE has the time component as well. Most probably the date in your database also has the time component.
Change your query to :
to see all records which were logged from 00:00 yesterday.
To see the actual timecomponents, use to char.