通过知道只有日期无时间从表中选择(ORACLE) [英] Select from table by knowing only date without time (ORACLE)
问题描述
我想通过知道列中包含日期和时间的日期来检索表中的记录。
I'm trying to retrieve records from table by knowing the date in column contains date and time.
假设我有一个名为 t1的表
其中只包含两列名称
和 date
。
Suppose I have table called t1
which contains only two column name
and date
respectively.
存储在列日期的数据像这样 8/3/2010 12:34:20 PM
。
The data stored in column date like this 8/3/2010 12:34:20 PM
.
我想通过这个查询来检索这个记录(注意我没有把时间):
I want to retrieve this record by this query for example (note I don't put the time):
Select * From t1 Where date="8/3/2010"
这个查询给我什么!
如何仅通过了解 date
来检索 date
没有时间?
How can I retrieve date
by knowing only date
without the time?
推荐答案
DATE
是Oracle中的保留关键字,所以我正在使用列名称 your_date
。
DATE
is a reserved keyword in Oracle, so I'm using column-name your_date
instead.
如果您在 your_date
,我会使用
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
或 BETWEEN
:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
如果没有索引或没有太多的记录
If there is no index or if there are not too many records
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
应该足够了。 TRUNC
无参数从 DATE
中删除小时,分钟和秒。
should be sufficient. TRUNC
without parameter removes hours, minutes and seconds from a DATE
.
如果性能非常重要,请考虑将 基于函数的索引
If performance really matters, consider putting a Function Based Index
on that column:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
这篇关于通过知道只有日期无时间从表中选择(ORACLE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!