简体   繁体   中英

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)"

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.

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. 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):

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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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