简体   繁体   English

Oracle:在日期时间过滤查询

[英]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). 为了实现我由于选择时间戳而使用的时间戳(如果我改用datetime,那么我从select中得不到任何信息,可能是因为我使用的格式在第二秒修剪了datetime)。

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 ORA-01830:日期格式图片在转换整个输入字符串之前结束

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. 如果您只想比较并检查他的日期,则在LHS和RHS上都使用trunc。

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. 您可以在查询中使用“ TRUNC”和“ IN”关键字的组合来实现期望的结果。 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 !! 干杯!

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

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