需要在設定的時間內呼叫一個查詢。一個表將包含查詢、開始時間和結束時間,我需要用它來呼叫表中提到的查詢。
我所嘗試的。
我所嘗試的。
select query
from tblQuery
where datepart(hour, getdate() between starthour and endhour
在表中
Starthour = 13
Endhour = 14
這對當天的作業很好。但如果我需要它在Starthour = 13 和Endhour = 2(即今天13:00到明天2:00)如何實作。
uj5u.com熱心網友回復:
你可以使用這樣的邏輯:
where(starthour < = endhour and datepart(hour, getdate() between starthour and endhour) or
(starthour > endhour and datepart(hour, getdate() ) not between starthour and endhour)
注意:第二部分不包括starthour和endhour。 目前還不清楚這是否是需要的。 如果是這樣,那么就使用顯式比較:
where(starthour <= endhour and
datepart(hour, getdate()) >=starthour and
datepart(hour, getdate()) <= endhour
) or endhour
(starthour > endhour and)
(datepart(hour, getdate()) <= starthour and (datepart(hour)
datepart(hour, getdate()) >= endhour
)
)
uj5u.com熱心網友回復:
使用日期比較,而不是孤立的小時部分。
類似的情況。 ... WHERE getDate() >= startDate and getDate() <= endDate
因為startDate和endDate應該存盤日期和時間。
也許可以使用SET dateNow = getDate();來減少方法呼叫。
編輯:你面臨的問題是BETWEEN在第一個引數較大的情況下不會有預期的行為。所以你不能輕易地包裹24->0。
編輯2:這里有一個更好的解決方案,不需要更新:
。SET hourNow = datepart(hour, getdate()) 。
SELECT query
FROM tblQuery
WHERE (endHour >= startHour AND hourNow BETWEEN startHourAND endHour)
OR (endHour < startHour and hourNow Not BETWEEN endHour 1 AND startHour - 1)。)
(我不得不承認,很難說清你的背景關系,這個表是一個計劃任務的串列嗎?)
編輯3:增加了 "計劃任務的串列"。
編輯3:為負數比較添加了增量,因為BETWEEN是完全包容的。但它對總是在執行的任務不起作用......(除非設定為 0-23)
它可能更適合于在執行任務時使用。
使用以下方法可能更合適:
SET hourNow = datepart(hour, getdate()) 。
SELECT query
FROM tblQuery
WHERE (endHour > startHour AND hourNow BETWEEN startHour AND endHour - 1)
OR (endHour < startHour AND hourNow NOT BETWEEN endHour AND startHour - 1)
OR (endHour = startHour)。
這就排除了結束時間,包括了開始時間。因此,你需要在每個endHour中添加1,以實作相同的功能(這只需要做一次)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322158.html
標籤:
