如何在我的SQL调用中说明日期范围为07/01/2013 - 09/30/2013的特定时间(从0500时至0600)?
我想使用MOPACTIVITY.MOPSTART字段。这个例子显示了07/01 - 09/30的所有内容。我只需要0500hrs - 0600hrs在同一日期范围内的那些。
谢谢!
FROM MOPUSER.MOPACTIVITY
WHERE TO_CHAR(MOPACTIVITY.MOPSTART, 'yyyy-mm-dd hh24:mi') BETWEEN '2013-07-01 00:00:01' AND '2013-09-30 23:59:59'
答案 0 :(得分:3)
做两个比较,一个用于日期,一个用于时间:
FROM MOPUSER.MOPACTIVITY
WHERE TO_CHAR(MOPACTIVITY.MOPSTART, 'yyyy-mm-dd') BETWEEN '2013-07-01' AND '2013-09-30' and
TO_CHAR(MOPACTIVITY.MOPSTART, 'hh24:mi') BETWEEN '05:00' AND '05:59'
您还可以使用hour()
简化第二个条件:
FROM MOPUSER.MOPACTIVITY
WHERE TO_CHAR(MOPACTIVITY.MOPSTART, 'yyyy-mm-dd') BETWEEN '2013-07-01' AND '2013-09-30' and
hour(MOPACTIVITY.MOPSTART) = 5
答案 1 :(得分:0)
我是怎么做到的。我更喜欢@Gordon Linoff的答案!
SELECT MOPACTIVITY.MOPID ,
TO_CHAR(MOPACTIVITY.MOPSTART, 'yyyy-mm-dd hh24:mi') AS MOPSTART_DATE,
TO_CHAR(MOPACTIVITY.MOPEND, 'yyyy-mm-dd hh24:mi') AS MOPEND_DATE,
MOPACTIVITY.MOPCREATOR,
MOPACTIVITY.MOPIMPLEMENTOR
FROM MOPUSER.MOPACTIVITY
WHERE TO_CHAR(MOPACTIVITY.MOPSTART, 'yyyy-mm-dd') BETWEEN '2013-07-01' AND '2013-09-30'
AND to_number(to_char(MOPACTIVITY.MOPSTART, 'sssss')) between 18000 and 21600
Order by MOPACTIVITY.MOPID
答案 2 :(得分:0)
可能会在MOPSTART
上利用索引(如果有的话)的方法是这样的:
WHERE
MOPACTIVITY.MOPSTART >= DATE '2013-07-01'
AND
MOPACTIVITY.MOPSTART < DATE '2013-09-30' + 1
AND
TO_CHAR(MOPACTIVITY.MOPSTART, 'HH24') = '05'
这假设您不想要包含06:00。