Oracle 11g 中的日期比较

发布于 2024-12-23 14:46:02 字数 224 浏览 5 评论 0原文

我是 Oracle 11g 的新手我有一个关于查询的问题。

我有一个表dummy,其中有created_date类型为Date的列。

我想要一个查询,该查询将返回 created_date + 7 天小于今天日期的所有记录。

Oracle 11g 中什么类型的查询可以完成此任务?

I'm new to Oracle 11g & I have a question about a query.

I have a table dummy which has created_date column of type Date.

I want a query which will return all the records where created_date + 7 days is less than today's date.

What type of query in Oracle 11g accomplishes this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

方圜几里 2024-12-30 14:46:02

Oracle 允许您使用 + 进行日期算术,因此

where table.created_date >= sysdate
and table.created_date < sysdate + 7

将查找正好现在和正好现在加上 7 天之间的行。

如果您不想包含时间部分,可以使用 trunc() 函数

where trunc(table.created_date) >= trunc(sysdate)
and trunc(table.created_date) < trunc(sysdate) + 7

Oracle lets you use + for date arithmetic, so

where table.created_date >= sysdate
and table.created_date < sysdate + 7

will find rows between exactly now and exactly now plus 7 days.

If you don't want to include the time component, you can use the trunc() function

where trunc(table.created_date) >= trunc(sysdate)
and trunc(table.created_date) < trunc(sysdate) + 7
梦年海沫深 2024-12-30 14:46:02

我认为rejj的解决方案会给你从现在到未来7​​天的记录。从您的描述来看,您可能想要过去 7 天内的记录。我会这样做:

WHERE created_date <= SYSDATE
  AND created_date >= SYSDATE - 7

这可能更清楚,并且是等效的:

WHERE created_date BETWEEN SYSDATE AND (SYSDATE - 7)

请注意,使用 TRUNC() 将导致优化器绕过您在created_date上拥有的任何索引,除非您定义了功能索引。

I think rejj's solution will give you the records between now and seven days in the future. From your description, it sounds like you probably want those records within the past seven days. I'd do this as:

WHERE created_date <= SYSDATE
  AND created_date >= SYSDATE - 7

This may be more clear, and is equivalent:

WHERE created_date BETWEEN SYSDATE AND (SYSDATE - 7)

Be aware that using TRUNC() will cause the optimizer to bypass any indexes you have on created_date, unless you have a functional index defined.

时光倒影 2024-12-30 14:46:02

一样。

SELECT *
  FROM DUMMY
  WHERE CREATED_DATE >= TRUNC(SYSDATE) - INTERVAL '7' DAY

使用 INTERVAL 常量可以使日期算术更清晰,就像共享和享受

Using INTERVAL constants can make date arithmetic clearer, as in

SELECT *
  FROM DUMMY
  WHERE CREATED_DATE >= TRUNC(SYSDATE) - INTERVAL '7' DAY

Share and enjoy.

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