【问题标题】:TO_DATE function Oracle SQL [duplicate]TO_DATE 函数 Oracle SQL [重复]
【发布时间】:2014-02-01 15:00:55
【问题描述】:

我正在尝试从某个范围内选择某个日期的数据,但不在某个参数集中。

目前:

SELECT external_reporting_id AS asin,
       last_updated          AS "Received"
  FROM transfer_delivery_items
 WHERE last_updated >= TO_DATE(sysdate, 'DD-MON-YY') - 13
 ORDER BY last_updated DESC;

但我希望查看不在该日期范围内的所有内容。
我已经尝试过 >= TO_DATE(sysdate, 'DD-MON-YY')-13,但它看起来不像是经过批准的格式,或者如果有什么可以帮助我的话,甚至是可能的会很棒的。

【问题讨论】:

  • to_date()string 转换为 datesysdate is 已经是约会对象了。因此,将to_date() 应用于date 值将convert 应用于date 值是完全没用的。实际上更糟:因为这种方式 datesysdate 首先被转换为字符串值(受隐式 NLS 设置的影响),然后再次转换回 date
  • 我的印象是我必须转换为日期格式。当我删除 sysdate 时,我收到错误,当我删除 sysdate 和“DD-MON-YYYY”时,我收到错误。所以,我一直在这个数据库上使用它们。
  • A date 没有“格式”。所以没有必要“转换”它。在您的情况下,last_updated >= sysdate - 13 应该可以正常工作。可能最好使用trunc(last_updated) >= trunc(sysdate) - 14 来忽略date 值的时间部分。
  • 明白了,谢谢!解决了这个问题。
  • 这行得通吗?
    从 transfer_delivery_items 中选择 external_reporting_id ASIN,last_updated 为“已接收”,其中 last_updated = trunc(sysdate)+13 order by last_updated描述;

标签: sql oracle to-date


【解决方案1】:

sysdate 已经是一个日期,所以如果你想提前 13 天做所有事情:

select external_reporting_id ASIN, last_updated as "Received" 
  from transfer_delivery_items
 where last_updated >= trunc(sysdate) -13
 order by last_updated desc;

trunc(sysdate) 将从日期中删除时间,以包含 13 天前的所有记录。

注意:我不会这样做

trunc(last_updated) >= trunc(sysdate) 

因为last_updaetd 可能是一个索引列,而trunc(last_update) 将绕过该索引。 (除非trunc(last_updated)上有函数索引。

更新:

要获得提前 13 天且不超过未来 13 天的日期块,请执行以下操作:

select external_reporting_id ASIN, last_updated as "Received" 
  from transfer_delivery_items
 where last_updated >= trunc(sysdate) -13
   and last_updated <= trunc(sysdate) +13
 order by last_updated desc;

【讨论】:

  • 如果我至少提前 13 天和至少提前 13 天想要所有东西怎么办。今天+13 和今天-13 停电?
【解决方案2】:

它认为你不能介于两者之间:http://docs.oracle.com/cd/B28359_01/server.111/b28286/conditions011.htm#SQLRF52147

我没有可用的 Oracle 实例,否则我会对此进行测试。我很确定它会起作用。

【讨论】:

  • 不会指定我设置日期范围吗?或者,我想要什么日期?
  • 当我这样做时:last_updated 不在 trunc(sysdate) 和 trunc(sysdate)-13 之间我仍然收到日期之间的数据嗯..
  • 我建议您暂时忘记 sysdate、trunc 等。我认为您没有使用您认为正在使用的 DATE 值。 BETWEEN 一直为我工作。
猜你喜欢
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多