2013-03-19 76 views
2

你能幫助我,我正在使用下面的SQL視圖(然後我使用晶體報告)。我需要日期差異(分鐘),但現在我需要排除週末。 請幫助:)SQL - datediff(分鐘)排除週末

SELECT intwc        AS wc, 
     Datediff(n, start_date, end_date) AS time, 
     mh_start_date      AS date, 
     'Repair'       AS type 
FROM dbo.xxxxxxx 
+0

您正在使用哪些DBMS? – 2013-03-19 13:30:36

+1

可能重複[計數工作日之間](http://stackoverflow.com/questions/252519/count-work-days-between-two-dates-in-t-sql) – 2013-03-19 13:38:33

+0

只是爲了確認,對於'星期五晚上11:30和星期一上午12:30你想看60分鐘嗎? – bendataclear 2013-03-19 13:39:22

回答

0
SELECT intwc        AS wc, 
    Datediff(n, start_date, end_date) AS time, 
    mh_start_date      AS date, 
    'Repair'       AS type 
FROM dbo.xxxxxxx 
Where DATEPART(dw, start_date) NOT IN (1, 7) and DATEPART(dw, end_date) NOT IN (1, 7) 
1

這個答案假定您要排除在一分鐘基礎上週末,也是它完全基於答案this question

DECLARE @StartDate DATETIME 
DECLARE @EndDate DATETIME 
SET @StartDate = '2013/03/15 23:30:00' 
SET @EndDate = '2013/03/18 00:30:00' 


SELECT 
    (DATEDIFF(MINUTE, @StartDate, @EndDate)) 
    -(DATEDIFF(wk, @StartDate, @EndDate) * (2*24*60)) 
    -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN (24*60) ELSE 0 END) 
    -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN (24*60) ELSE 0 END) 
+1

無論StartDate或EndDate是什麼時間,最後兩行都會減去整個24 * 60分鐘。但是,如果例如EndDate在星期六的0:30,那麼應該減去30分鐘,而不是24 * 60。在你的代碼中,嘗試將EndDate更改爲'2013/03/17 00:30:00',它會給出一個負數。 – wwv 2013-08-05 19:38:25

0

所以你需要能夠處理一些CASE語句來處理所有邊界情況。這是我整理的一個例子。該數字表只是一個理貨表,在這種情況下,1至30

CREATE TABLE #times (id INT IDENTITY(1,1), start_stamp DATETIME, end_stamp DATETIME) 

INSERT INTO #times 
     ( 
      start_stamp , 
      end_stamp 
     ) 
SELECT DATEADD(DAY, -2*Number, CURRENT_TIMESTAMP), DATEADD(DAY, -1*Number, CURRENT_TIMESTAMP) 
FROM Common.NUMBERS 
WHERE Number < 31 

SELECT id, start_stamp, end_stamp, 
CASE WHEN DATEDIFF(DAY, start_stamp, end_stamp) < 7 THEN 
    CASE WHEN DATEPART(weekday, start_stamp) < DATEPART(weekday, end_stamp) 
     THEN DATEDIFF(MINUTE, start_stamp, DATEADD(HOUR, -48, end_stamp)) 
     ELSE DATEDIFF(MINUTE, start_stamp, end_stamp) END 
    ELSE DATEDIFF(MINUTE, start_stamp, DATEADD(HOUR, -48*(DATEDIFF(WEEK, start_stamp, end_stamp)), end_stamp)) END 
    + CASE WHEN DATENAME(weekday,start_stamp) IN ('Sunday', 'Saturday') THEN 1440 ELSE 0 END 
    + CASE WHEN DATENAME(weekday,end_stamp) IN ('Sunday', 'Saturday') THEN 1440 ELSE 0 END 
FROM #times 

有可能是一個更優雅的方式來做到這一點,但代碼可以讓你設置對整個結果運行,每行計算。

4

這是@ bendataclear的答案的修改版本。它直接計算週末分鐘數,而不是計算日數並乘以24 * 60。它也佔了出發的所有4個組合/週六/週日結束

我使用CONVERT(date,@StartDate)得到的@StartDate日期與00:00:00一段時間,然後將其用於計算部分週六,週日。有這樣做的better ways,但我走最簡單。

DECLARE @StartDate DATETIME 
DECLARE @EndDate DATETIME 
SET @StartDate = '2013/03/15 23:30:00' 
SET @EndDate = '2013/03/17 00:30:00' 


SELECT 
( DATEDIFF(MINUTE, @StartDate, @EndDate) 
    - (DATEDIFF(wk, @StartDate,@EndDate)*(2*24*60) 
     -- End on Sunday 
     -(CASE WHEN DATEPART(dw, @EndDate) = 1 THEN 24.0*60-DATEDIFF(minute,CONVERT(date,@EndDate),@EndDate) ELSE 0 END) 
     -- Start on Saturday 
     -(CASE WHEN DATEPART(dw, @StartDate) = 7 THEN DATEDIFF(minute,CONVERT(date,@StartDate),@StartDate) ELSE 0 END) 
     -- End on Saturday 
     +(CASE WHEN DATEPART(dw, @EndDate) = 7 THEN DATEDIFF(minute,CONVERT(date,@EndDate),@EndDate) ELSE 0 END) 
     -- Start on Saturday 
     +(CASE WHEN DATEPART(dw, @StartDate) = 1 THEN 24.0*60-DATEDIFF(minute,CONVERT(date,@StartDate),@StartDate) ELSE 0 END) 
    ) 
) 
+0

偉大的解決方案,評論是錯誤的壽。 – Behrens 2015-05-11 10:06:32

+0

對於額外的分數,/ 60 60這個答案會給你兩個日期之間的總小時數,%60會給你總分鐘數。 (結果向上舍入) – 2017-01-22 18:42:11