Pergunta

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:

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

Foi útil?

Solução

Just enclose all dates in a TRUNC function if you are not bothered with the time as you say.

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

CREATE INDEX my_index ON person_log (TRUNC(work_time))

Outras dicas

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')
__________________^
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top