简体   繁体   中英

Oracle: filter query on datetime

I need to restrict a query with a

SELECT ... FROM ... 
WHERE my_date=(RESULT FROM A SELECT)
...  ;

in order to achieve that I am using as result of the select a timestamp (if I instead use a datetime I get nothing from my select probably because the format I am using trims the datetime at the second).

Sadly this is not working because these kindo of queries:

select DISTINCT TO_DATE(TO_TIMESTAMP(TO_DATE('25-10-2017 00:00', 'dd-MM-yyyy HH24:MI'))) from DUAL;

return an

ORA-01830: date format picture ends before converting entire input string

how to deal with timestamp to date conversion?

If you want to just compare and check only he dates use trunc on both LHS and RHS.

SELECT ... FROM ... 
WHERE trunc(my_date)=(select trunc(RESULT) FROM A)
...  ;

This will just compare the dates by truncating the timestamp values

You can use the combination of "TRUNC" and "IN" keywords in your query to achieve what you are expecting. Please check the below query sample as a reference.

SELECT * FROM customer WHERE TRUNC(last_update_dt) IN (select DISTINCT (TRUNC(last_update_dt)) from ... )

Cheers !!

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