根据 SYSDATE 获取列值

发布于 2024-12-19 03:04:15 字数 684 浏览 1 评论 0原文

我有一个表,其中有 2 列。定义是

CREATE TABLE LOGGING_T
(
  TSTAMP  DATE,
  LINE    VARCHAR2(300)
)
TABLESPACE OPERATIONS
MONITORING
/

TSTAMP 列具有诸如 30-NOV-1129-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)

,那么它会获取 TSTAMP30-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-12-26 03:04:15

DATE 包含一天中的时间和日期。

如果 SYSDATE2011-12-01 1:18:00 PM,则 SYSDATE-1 将为 2011-11-30 1 :18:00 下午。

您期望从 11 月 30 日开始查找的行是在时间元素之前还是之后?

如果您不关心时间,只想根据日期进行过滤,则可以使用 TRUNC()

select * 
  from LOGGING_T 
 where TRUNC(TSTAMP) >= TRUNC(SYSDATE - 1);

您可能想也可能不想确定比较的双方运算符被 TRUNC() 编辑,因为 TRUNC() 只会强制日期的时间元素为午夜。

select to_char(trunc(sysdate), 'YYYY-MM-DD HH:MI:SS PM') 
  from dual;

NOW
----------------------
2011-12-01 12:00:00 AM

A DATE contains time of day as well as the date.

If SYSDATE was 2011-12-01 1:18:00 PM then SYSDATE-1 would be 2011-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():

select * 
  from LOGGING_T 
 where TRUNC(TSTAMP) >= TRUNC(SYSDATE - 1);

You'll may or may not want to make sure both sides of your comparison operator are TRUNC()ed because TRUNC() will just force the time element of the date to be midnight.

select to_char(trunc(sysdate), 'YYYY-MM-DD HH:MI:SS PM') 
  from dual;

NOW
----------------------
2011-12-01 12:00:00 AM
少女净妖师 2024-12-26 03:04:15

值 SYSDATE 也包含时间部分。数据库中的日期很可能也包含时间部分。

将查询更改为 :

select * from LOGGING_T where TSTAMP >= TRUNC(SYSDATE - 1)

以查看从昨天 00:00 开始记录的所有记录。

要查看实际的时间分量,请使用 char。

SQL> select sysdate from dual;

SYSDATE
---------
01-DEC-11

  1* select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS') date1 from dual
SQL> /

DATE1
--------------------
01-Dec-2011 16:29:01

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 :

select * from LOGGING_T where TSTAMP >= TRUNC(SYSDATE - 1)

to see all records which were logged from 00:00 yesterday.

To see the actual timecomponents, use to char.

SQL> select sysdate from dual;

SYSDATE
---------
01-DEC-11

  1* select to_char(sysdate,'DD-Mon-YYYY HH24:MI:SS') date1 from dual
SQL> /

DATE1
--------------------
01-Dec-2011 16:29:01
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文