2012-07-13 182 views
0

請有人可以幫助我在這裏,我試圖得到日期範圍之間的小時間隔表。獲取日期範圍之間的時間間隔

例如:

if starttime == 2012-02-06 23:59:00' 
then endtime = '2012-02-29 10:26:17' 

我應該有一個表(變量表)有這樣的內容:

Start     End      Seconds 
2012-02-06 23:59:00.000 2012-02-06 23:59:00.000 0 
2012-02-29 09:00:00.000 2012-02-29 09:59:00.000 3540 
2012-02-29 10:00:00.000 2012-02-29 10:26:17.000 1577 
+0

糾正我,如果我錯了 - 小時列似乎是錯誤的我。在兩種情況下都應該爲零。 – 2012-07-13 10:24:46

+0

請檢查更新 – 2012-07-13 10:40:59

+0

請檢查@LolCoder的答案。 – 2012-07-13 10:41:41

回答

0

謝謝大家的建議和意見。我終於得到了一個是解決可能的問題。

下面是腳本的解決方案,我想出了:

DECLARE @start_date datetime = CONVERT(DATETIME,'2012-02-06 23:59:01.000',20); 
DECLARE @end_date datetime = CONVERT(DATETIME,'2012-12-08 23:59:17.000',20); 
DECLARE @org datetime ; 
DECLARE @end datetime ; 
DECLARE @datetable TABLE (h_start datetime, h_end datetime,h_sesc int); 

WHILE (dateadd(second, -1, dateadd(hour, datediff(hour, 0, @start_date)+1, 0))) < @end_date 
BEGIN 
SET @org = null; 
SET @org = @start_date; 
SET @end = (dateadd(second, -1, dateadd(hour, datediff(hour, 0, @org)+1, 0))); 
INSERT INTO @datetable (h_start, h_end,h_sesc) 
VALUES(dateadd(second, 0,@org), @end,DATEDIFF(second, @org,@end)); 

SET @start_date = dateadd(second, 1,@end); 

END; 


INSERT INTO @datetable (h_start, h_end,h_sesc) 
VALUES(dateadd(second, 0,@start_date), @end_date,DATEDIFF(second, dateadd(second, 0,@start_date),@end_date)); 

SELECT * FROM @datetable; 

以上將給出如下因素的結果:

h_start     h_end     h_sesc 
2012-02-06 23:59:01.000 2012-02-06 23:59:59.000 58 
2012-02-07 00:00:00.000 2012-02-07 00:59:59.000 3599 
2012-02-07 01:00:00.000 2012-02-07 01:59:59.000 3599 
2012-02-07 02:00:00.000 2012-02-07 02:59:59.000 3599 
2012-02-07 03:00:00.000 2012-02-07 03:59:59.000 3599 
2012-02-07 04:00:00.000 2012-02-07 04:59:59.000 3599 
2012-02-07 05:00:00.000 2012-02-07 05:59:59.000 3599 

.. ..

2012-12-08 18:00:00.000 2012-12-08 18:59:59.000 3599 
2012-12-08 19:00:00.000 2012-12-08 19:59:59.000 3599 
2012-12-08 20:00:00.000 2012-12-08 20:59:59.000 3599 
2012-12-08 21:00:00.000 2012-12-08 21:59:59.000 3599 
2012-12-08 22:00:00.000 2012-12-08 22:59:59.000 3599 
2012-12-08 23:00:00.000 2012-12-08 23:59:17.000 3557 

希望有人會發現它有用。

1

有了期待您的預計表,你要在兩個時間之間獲得型差分在second

SELECT DATEDIFF(SECOND, '2012-02-29 09:00:00.000', '2012-02-29 09:59:00.000')只給出3540是第二個的差異。

爲了得到小時的時差,你可以試試這個代碼:

SELECT DATEDIFF(hour, '2012-02-06 23:59:00.000', '2012-02-06 23:59:00.000') 
+0

我希望結果分解爲小時間隔和間隔時間之間的小時數 – 2012-07-13 10:28:14

+0

請閱讀更新的答案...您需要獲得第一個小時的差異,然後得到第二個差異,這是我在答案中更新的。 – 2012-07-13 10:29:56

+0

對不起,應該是秒 – 2012-07-13 10:30:01

0
DECLARE @start_date datetime = CONVERT(DATETIME,'2012-02-06 23:59:01.000', 20); 
DECLARE @end_date datetime = CONVERT(DATETIME,'2012-12-08 23:59:17.000', 20); 

-- Using a recursive query after round down to full hours avoids a loop. The resulting intervals should be easier to use that way, e.g. you can but don't have to insert them into a table variable. 
SET @start_date = DATEADD(HOUR, DATEPART(HOUR, @start_date), CAST(FLOOR(CAST(@start_date AS float)) AS datetime)); 
SET @end_date = DATEADD(HOUR, DATEPART(HOUR, @end_date), CAST(FLOOR(CAST(@end_date AS float)) AS datetime)); 
WITH RecursiveTimeIntervals AS 
(SELECT @start_date Interval 
UNION ALL SELECT DATEADD(HOUR, 1, r.Interval) 
FROM RecursiveTimeIntervals r 
WHERE r.Interval < @end_date) 
SELECT * 
FROM RecursiveTimeIntervals 
ORDER BY 1 
OPTION (MAXRECURSION 0) 
相關問題