简体   繁体   English

如何在sqldeveloper中为日期时间过滤数据库

[英]how to filter a db for datetime in sqldeveloper

I need to filter a table for specific, accurate to the second, timeperiods. 我需要为特定的时间间隔精确筛选表。 Datatype is "TIMESTAMP (6)" 数据类型为“ TIMESTAMP(6)”

select *
from table
where trunc(load_date)<=to_date ('10.08.15 15:10:58', 'dd.mm.yy hh24:mi:ss')
and trunc(load_date)>=to_date ('10.08.15 15:11:08', 'dd.mm.yy hh24:mi:ss');

This is what if come to. 这是怎么回事。 But i seem to miss smt. 但我似乎想念smt。

select *
from b_bis_donexa_delta_2
where trunc(load_date)=to_date ('10.08.15', 'dd.mm.yy');

This works perfectly fine. 这工作得很好。 But scolling through the results isn't very efficient. 但是汇总结果并不是很有效。

atm i get no results, but no error message. atm我没有结果,但是没有错误消息。 but there IS at least one result. 但至少有一个结果。 i even tried to swap those < = > randomly because i thought i lost my mind. 我什至试图随机交换这些<=>,因为我以为我迷失了方向。

Doing trunc(load_date) truncates the time to midnight on that value's day (and also converts to a date, rather than a timestamp): 进行trunc(load_date)时间截断到该值当天的午夜(并且还转换为日期,而不是时间戳):

select systimestamp, trunc(systimestamp) from dual;

SYSTIMESTAMP                         TRUNC(SYSTIMESTAMP)
------------------------------------ -------------------
2019-09-10 12:53:54.400453000 +01:00 2019-09-10 00:00:00

Once you've truncated your load_date , that midnight time is not within your target range. 截断load_date ,该午夜时间不在目标范围内。 In your second version you are comparing the truncated value with a time which is also at midnight, hence it now finding a match - but it may or may not be in your 10-second window (there's no way to tell), and also may prevent an index on that column being used - which is probably why it's slow. 在您的第二个版本中,您正在将截断后的值与也在午夜的时间进行比较,因此现在可以找到匹配项-但它可能在或可能不在您的10秒窗口中(无法分辨),并且可能防止使用该列上的索引-这可能是为什么它很慢的原因。

Don't truncate; 不要截断; and I'd compare against the same data type: 我将比较相同的数据类型:

select *
from table
where load_date >= to_timestamp ('10.08.15 15:10:58', 'dd.mm.yy hh24:mi:ss')
and load_date <= to_timestamp ('10.08.15 15:11:08', 'dd.mm.yy hh24:mi:ss');

or preferably using 4-digit years: 或最好使用4位数的年份:

select *
from table
where load_date >= to_timestamp ('10.08.2015 15:10:58', 'dd.mm.yyyy hh24:mi:ss')
and load_date <= to_timestamp ('10.08.2015 15:11:08', 'dd.mm.yyyy hh24:mi:ss');

or even timestamp literals if these are fixed values: 甚至是时间戳字面量(如果它们是固定值):

select *
from table
where load_date >= timestamp '2015-08-10 15:10:58'
and load_date <= timestamp '2015-08-10 15:11:08';

Also check that you do really want both >= and <= ; 还要检查您是否确实确实想要>=<= if you are getting multiple 10-second ranges then you may actually want >= and < to avoid the exact time appearing in two ranges. 如果要获得多个10秒范围,则实际上可能需要>=<以避免确切的时间出现在两个范围内。

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

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