简体   繁体   English

如何从SQL表中选择WHEST TIMESTAMP是一个特定的Date

[英]How to select from SQL table WHERE a TIMESTAMP is a specific Date

Im trying to do a query where a TIMESTAMP field is = to a specific date but my query is not working: 我试图进行查询,其中TIMESTAMP字段=特定日期但我的查询不起作用:

The field is type TIMESTAMP(6) which I have only ever worked with DATE / DATETIME fields before. 该字段是TIMESTAMP(6)类型,我之前只使用过DATE / DATETIME字段。 Here is example of a value stored here: 04-OCT-13 12.29.53.000000000 PM 以下是此处存储的值的示例: 04-OCT-13 12.29.53.000000000 PM

Here is my SELECT statement: 这是我的SELECT语句:

SELECT * FROM SomeTable WHERE timestampField = TO_DATE('2013-10-04','yyyy-mm-dd')

I am retrieving no results and I am assuming it has to do with the fact that its not matching the TIME portion of the timestamp 我没有检索到任何结果,我假设它与时间戳的TIME部分不匹配这一事实

If you want every record that occurs on a given day then this should work: 如果您想要在给定日期发生的每条记录,那么这应该有效:

SELECT * FROM SomeTable
  WHERE timestampField >= TO_TIMESTAMP( '2013-03-04', 'yyyy-mm-dd' )
    AND timestampField < TO_TIMESTAMP( '2013-03-05', 'yyyy-mm-dd')

That will be likely to take advantage of an index on timestampField if it exists. 如果存在,那么可能会利用timestampField上的索引。 Another way would be: 另一种方式是:

SELECT * FROM SomeTable
  WHERE TRUNC(timestampField) = TO_DATE( '2013-03-04', 'yyyy-mm-dd' )

in which case you may want a function-based index on TRUNC(timestampField) . 在这种情况下,您可能需要TRUNC(timestampField)上基于函数的索引。

(Note that TRUNC applied to a TIMESTAMP returns a DATE.) (注意,应用于TIMESTAMP的TRUNC返回DATE。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM