【问题标题】:Select rows where day is less than today选择日期小于今天的行
【发布时间】:2014-04-26 21:53:45
【问题描述】:

如何在 Oracle DB 中从昨天开始选择过去的行,其中 created_date 之类的字段是 timestamp(6)

我不想比较时间,只想比较日期。

【问题讨论】:

标签: sql oracle


【解决方案1】:

如果您希望恰好在当前时间前一天:

select *
from table t
where created_date < sysdate - 1;

如果您想要今天之前的时间:

select *
from table t
where created_date <= trunc(sysdate);

【讨论】:

    【解决方案2】:

    来自Oracle documentation on SELECT

    SELECT * FROM orders
      WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
    

    我可以从我的应用程序中传递这种日期格式,就像一个魅力。

    【讨论】:

      【解决方案3】:

      如您只想比较日期:

      select *
      from table t
      where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
      

      【讨论】:

      • 减 1 表示不使用 oracle 函数以及在 where 子句中的 created_date 字段上使用函数。这会减慢生产速度。
      • @DanBracuk:Oracle 支持Function-based indexes,提问者只想比较日期。我在 Oracle 文档中看到了 DATE_SUB:[docs.oracle.com/cd/E17952_01/refman-5.1-en/…
      【解决方案4】:

      您可以使用转换函数将时间戳处理为日期:

      SELECT cast(SYSTIMESTAMP(6) as date)
      FROM   dual;
      

      因此您可以选择日期为“昨天”的行:

      select ....
      where cast(SYSTIMESTAMP(6) as date) like sysdate - 1 
      

      注意:将 SYSTIMESTAMP(6) 替换为具有时间戳类型的列名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-22
        • 2023-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-28
        • 2011-07-18
        • 1970-01-01
        相关资源
        最近更新 更多