简体   繁体   English

Oracle SQL-确定时间戳是否在一个范围内,不包括日期

[英]Oracle SQL - Determine if a timestamp falls within a range, excluding date

I'm looking for a way to determine if a timestamp falls between two times, regardless of the date in that timestamp. 我正在寻找一种确定时间戳是否介于两次之间的方法,而不管该时间戳中的日期如何。 So for example, if the time in the timestamp falls between '00:00:00.000' (midnight) and '01:00:00.000' (1 AM), I'd want to select that row regardless of the particular date. 因此,例如,如果时间戳记中的时间介于“ 00:00:00.000”(午夜)和“ 01:00:00.000”(凌晨1点)之间,则我希望选择该行,而不考虑特定的日期。

I've tried lots of different variations on the to_char and to_date functions, but I keep getting errors. 我在to_charto_date函数上尝试了许多不同的变体,但是我不断出错。 Coming from Informix, Oracle seems much more complicated. 来自Informix的Oracle似乎要复杂得多。

The thing closest to "correct" (I think) that I've tried is: 我尝试过的最接近“正确”(我认为)的是:

SELECT *
FROM my_table
WHERE SUBSTR(TO_CHAR(my_timestamp), 10) > '00:00:00.000'
AND SUBSTR(TO_CHAR(my_timestamp), 10) < '01:00:00.000'

... But nothing works. ...但是没有用。 Any tips or tricks? 有技巧或窍门吗?

I found a way to do it, but I'd still prefer something a little less hacky, if it exists. 我找到了一种方法,但是我仍然更喜欢一些不那么黑的东西(如果有的话)。

SUBSTR(SUBSTR(TO_CHAR(my_timestamp), 11), 0, 12) > '01.00.00.000'

Your solution looks correct to me except I haven't tried substr function. 您的解决方案对我来说看起来是正确的,除了我还没有尝试过substr函数。 This is what I used in one of my previous project: 这是我在上一个项目中使用的:

select * from orders 
   where to_char(my_timestamp,'hh24:mi:ss.FF3') 
   between '00:00:00.000' and '01:00:00.123';

Use TRUNC(my_timestamp, 'J') to remove the hours and get only the '2013-08-15 00:00:00.00'. 使用TRUNC(my_timestamp, 'J')删除小时并仅获取'2013-08-15 00:00:00.00'。 So: 所以:

WHERE my_timestamp - TRUNC(my_timestamp, 'J') > 0 
AND my_timestamp - TRUNC(my_timestamp, 'J') < 1/24 ;

As a variation on @kubanczyk's answer, since these are timestamps you get an interval when you subtract a value from its truncated form: 作为@kubanczyk答案的一种变体,由于这些是时间戳记,因此您从其截断形式中减去一个值时会得到一个间隔:

select systimestamp - trunc(systimestamp) from dual;

SYSTIMESTAMP-TRUNC(SYSTIMESTAMP)
---------------------------------------------------------------------------
+000000000 09:46:46.589795

Which isn't very helpful. 这不是很有帮助。 But if you're always looking for exact hours, as in your example, you can extract the hour number from that: 但是,如果您一直在寻找确切的小时数(例如您的示例),则可以从中提取小时数:

select extract (hour from systimestamp - trunc(systimestamp)) from dual;

EXTRACT(HOURFROMSYSTIMESTAMP-TRUNC(SYSTIMESTAMP))
-------------------------------------------------
                                                9

So in your example you could use: 因此,在您的示例中,您可以使用:

SELECT *
FROM my_table
WHERE EXTRACT(HOUR FROM my_timestamp - TRUNC(my_timestamp)) = 0

SQL Fiddle demo . SQL Fiddle演示

But, this will only be straightforward if the timeslots are exactly aligned with hours; 但是,只有在时隙与小时完全一致的情况下,这才是简单的。 otherwise you'd need to extract other elements too and the logic could get confusing, and @Ankit's approach will be simpler overall. 否则,您还需要提取其他元素,并且逻辑可能会造成混淆,并且@Ankit的方法总体上将更简单。

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

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