简体   繁体   English

Oracle SQL语句查找两个时间戳列之间具有输入时间戳的行

[英]Oracle SQL statement to find the row with input time stamp between two timestamp columns

I have a table in Oracle DB 我在Oracle DB中有一张表

|SHIFT|     START_TIME (TIMESTAMP)     |      END_TIME (TIMESTAMP)      |
|  2  |  01-AUG-15 11.00.00.000000 PM  |  02-AUG-15 08.00.00.000000 AM  |
|  4  |  01-AUG-15 07.00.00.000000 AM  |  01-AUG-15 04.00.00.000000 PM  | 
|  3  |  01-AUG-15 02.00.00.000000 AM  |  01-AUG-15 11.00.00.000000 AM  | 
|  1  |  01-AUG-15 02.00.00.000000 PM  |  01-AUG-15 11.00.00.000000 PM  | 
|  5  |  01-AUG-15 08.30.00.000000 AM  |  01-AUG-15 05.30.00.000000 PM  | 

I want those row(s) to be returned which correspond to a particular timestamp. 我希望返回与特定时间戳相对应的那些行。 Running the following query works: 运行以下查询有效:

select shift, 
  cast(start_time as time), 
  cast(end_time as time)
from mytable 
where cast('12.00.00.000000 PM' as time) 
  between cast(start_time as time) and
  cast(end_time as time);

BUT, the same query does not work for cast('12.00.00.000000 AM ' as time) ie the following query does not work. 但是,相同的查询不适用于演员表(时间为'12 .00.00.000000 AM '),即以下查询无效。 It returns 0 rows. 它返回0行。 What am I doing wrong? 我究竟做错了什么?

select shift, 
  cast(start_time as time), 
  cast(end_time as time)
from mytable 
where cast('12.00.00.000000 AM' as time) 
  between cast(start_time as time) and
  cast(end_time as time);

I even tried with the match between start_time and the start_time + 9hours but get the same behavior. 我什至尝试了start_time和start_time + 9hours之间的匹配,但是得到了相同的行为。

select shift,
cast(start_time as time), 
cast((start_time + numtodsinterval(9,'HOUR')) as time) 
from ata_ipd_web_shifts where cast('12.00.00.000000 PM' as time) 
BETWEEN  cast(start_time as time) AND 
cast((start_time + numtodsinterval(9,'HOUR')) as time);

So what is the issue here and what is the best way to extract the desired row(s)? 那么,这里的问题是什么?提取所需行的最佳方法是什么?

Make the time of day you want to check for an INTERVAL and add it to both the start date of the shift and the end date of the shift. 设置您要检查时间间隔的时间,并将其添加到班次的开始日期和班次的结束日期。 Like this: 像这样:

with d as ( SELECT 2 shift, to_timestamp('01-AUG-15 11.00.00.000000 PM') start_date, to_timestamp('02-AUG-15 08.00.00.000000 AM') end_date from dual
UNION ALL SELECT 4 shift, to_timestamp('01-AUG-15 07.00.00.000000 AM'), to_timestamp('01-AUG-15 04.00.00.000000 PM') from dual
UNION ALL SELECT 3 shift, to_timestamp('01-AUG-15 02.00.00.000000 AM'), to_timestamp('01-AUG-15 11.00.00.000000 AM') from dual
UNION ALL SELECT 1 shift, to_timestamp('01-AUG-15 02.00.00.000000 PM'), to_timestamp('01-AUG-15 11.00.00.000000 PM') from dual
UNION ALL SELECT 5 shift, to_timestamp('01-AUG-15 08.30.00.000000 AM'), to_timestamp('01-AUG-15 05.30.00.000000 PM') from dual
)
select * from d
where trunc(start_date)+TO_DSINTERVAL('0 09:00:00') between start_date and end_date
OR trunc(end_date)+TO_DSINTERVAL('0 09:00:00') between start_date and end_date

Note that the interval is 24-hour time, so midnight (that you used in your example) is 0 00:00:00. 请注意,时间间隔为24小时制,因此午夜(在示例中使用的时间)为0 00:00:00。

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

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