简体   繁体   English

oracle查询-如何从datetime和datetime相等?

[英]oracle query - how to equal from datetime and to datetime?

I am struggling to solve this problem in a query. 我正在努力解决查询中的这个问题。 I need both datetime (from and to) with the same date to get result with the same date(different time dont matter). 我需要具有相同日期的日期时间(从和到)都具有相同日期的结果(不同的时间无关紧要)。 How to solve that problem? 如何解决这个问题? Hope you can help me with this. 希望你能帮我这个忙。 I know it is a small thing but i spent the whole day working on it. 我知道这是一件小事,但我整天都在努力。 Dont understand what is problem. 不明白是什么问题。 Thanks! 谢谢!

oracle query: oracle查询:

SELECT *
FROM person p
   INNER JOIN person_log pl ON p.ID = pl.person_id
   INNER JOIN address ad ON ad.id = pl.vendor_id
WHERE LOWER(ad.city) = LOWER (:cityname)
and pl.work_time >= to_date('2014-04-12 11:30:06', 'YYYY-MM-DD HH24:MI:SS')    
and pl.work_time >= to_date('2014-04-12 11:30:06', 'YYYY-MM-DD HH24:MI:SS')

Should be result: 应该是结果:

Person | WorkTime 
Piet   | 2014/04/12 11:22:06,757037 PM
John   | 2014/04/12 11:25:08,954737 PM

My problem: 我的问题:

The result: different date 结果:不同的日期

To get data which belongs in the interval you must use 要获取属于该间隔的数据,您必须使用

and pl.work_time >= to_date('2014-04-12 11:22:06', 'YYYY-MM-DD HH24:MI:SS')    
and pl.work_time <= to_date('2014-04-12 11:30:00', 'YYYY-MM-DD HH24:MI:SS')
__________________^

Just enclose all dates in a TRUNC function if you are not bothered with the time as you say. 如果您不打扰您所说的时间,只需将所有日期括在TRUNC函数中即可。

and TRUNC(pl.work_time) >= TRUNC(to_date('2014-04-12 11:22:06', 'YYYY-MM-DD HH24:MI:SS'))    
and TRUNC(pl.work_time) <= TRUNC(to_date('2014-04-12 11:30:00', 'YYYY-MM-DD HH24:MI:SS'))

or depending on your situation 或根据您的情况

 and TRUNC(pl.work_time) <= TRUNC(:your_date_parameter)

Beware though, if you have an index on the work_time column then you will need to create a new index (function-based) as follows 但是请注意,如果在work_time列上有一个索引,则需要创建一个新的索引(基于函数),如下所示

CREATE INDEX my_index ON person_log (TRUNC(work_time))

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

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