【发布时间】:2015-09-04 17:49:56
【问题描述】:
我在 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 |
我希望返回与特定时间戳相对应的那些行。 运行以下查询有效:
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);
但是,相同的查询不适用于 cast('12.00.00.000000 AM' as time) 即以下查询不起作用。它返回 0 行。我做错了什么?
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);
我什至尝试了 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);
那么这里的问题是什么?提取所需行的最佳方法是什么?
【问题讨论】:
-
问题是您的时间跨度超过一天,但您只是比较一天中的时间。如果您的跨度超过一天,则需要进行不同的比较。
-
我试图在同一天保留它,但没有用。基本上我只需要比较时间。那么如何进行这种比较呢?希望有任何帮助;我不太擅长 SQL。