【问题标题】:Comparison of dates not retrieving actual data in sql querysql查询中未检索实际数据的日期比较
【发布时间】:2020-09-15 06:28:59
【问题描述】:

我正在尝试过滤掉日期“31-01-2020”之前和“01-01-2020”之后的数据,但以下查询不起作用。我错过了一些比较日期的东西吗?

select * 
from per_all_assignments_m paam
where 1 = 1
  and TO_CHAR(paam.effective_start_date, 'DD-MM-YYYY') <= '31-01-2020' 
  and TO_CHAR(paam.effective_END_date, 'DD-MM-YYYY') >= '01-01-2020'
  and assignment_number like '%555%'
  and assignment_type = 'E'

【问题讨论】:

  • 不要将日期转换为字符串进行比较。内置的日期功能已经足够了。

标签: sql date select oracle-sqldeveloper where-clause


【解决方案1】:

不要将日期转换为字符串进行比较。首先,您使用的格式使比较错误。即使您使用正确的日期格式,这仍然是非常低效的。

相反,将它们与文字日期进行比较:

where 
    effective_start_date >= date '2020-01-01'
    and effective_start_date < date '2020-02-01'

【讨论】:

  • 日期列的格式不同,因此我希望它们的格式相同
  • @Sree:您混淆了日期的显示格式及其实际的内部表示。您看到的格式只是一种显示格式,可以通过数据库或会话设置进行更改。但是您实际上有一列 date 数据类型,因此您需要将它与另一个 date 值进行比较(这就是上面的查询所做的)。
【解决方案2】:

你可以试试

select * from per_all_assignments_m paam
where 1=1
and (paam.effective_start_date <= to_date('31-01-2020', 'dd-mm-rrrr') OR paam.effective_END_date >= to_date('01-01-2020', 'dd-mm-rrrr'))
and assignment_number like '%555%'
and assignment_type = 'E'

【讨论】:

    猜你喜欢
    • 2013-09-16
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2012-08-02
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多