【问题标题】:Why is there a discrepancy between ActiveRecord SQL and PL/SQL Developer SQL?为什么 ActiveRecord SQL 和 PL/SQL Developer SQL 之间存在差异?
【发布时间】:2009-05-06 22:30:39
【问题描述】:

我在 Oracle PL/SQL Developer 中有一个 select 语句,它根据日期检索值:

select * from <table> where to_date(create_date) = to_date('20090506', 'YYYYMMDD')

其中create_date 是Oracle 类型的日期。这将返回一个非空集。但是,在 ActiveRecord 中:

<Table>.find_by_sql("select * from <table> where to_date(create_date) = to_date('20090506', 'YYYYMMDD')")

不返回任何行,这是不正确的。我怀疑这与 ActiveRecord 如何处理 Time/Date/DateTime 对象有关。

有什么想法吗? 谢谢

【问题讨论】:

    标签: sql oracle activerecord


    【解决方案1】:

    鉴于您自己的“hack”和列名 create-DATE,我怀疑 create-date 是一个 DATE 列。当您对日期列执行 to_date 时,Oracle 足够“友好”,不会引发错误,但如果确实引发错误会更好。 所以正确的解决方案是(在 ORACLE 中!和在 ActiveRecord 中一样):

    select * from <table> where create_date = to_date('20090506', 'YYYYMMDD')
    

    简而言之:不要在日期列上使用 to_date()。

    【讨论】:

      【解决方案2】:

      嗯,这是我的 hack。我并不为此感到自豪,但它会起作用。在我看来,实际上坚持 Ruby/Oracle Date 类型的任何更好的答案都会更好。

      Table.find_by_sql("
        select * from <table>
        where to_char(create_date, 'YYYY-MM-DD') = '2009-05-06'
      ")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 2021-09-11
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        相关资源
        最近更新 更多