如何計算SQL Server 2008R2中從表(從第1行到結尾)的兩個日期之間的計數分鐘數(分鐘計算是星期一上午7點到星期五晚上7點)?如何計算除週末和假期外的兩個日期之間的分鐘數?
我想這樣的事情,但晚上7點後,週一不起作用
DECLARE @StartDate DateTime='2015-06-12 13:03:00'
DECLARE @EndDate DateTime='2015-06-16 10:08:00'
IF DATEPART(WEEKDAY,@StartDate) = 1
SET @StartDate = DATEADD(HOUR,8,DATEADD(DAY,DATEDIFF(DAY,0,@StartDate),1))
IF DATEPART(WEEKDAY,@StartDate) = 7
set @StartDate = DATEADD(HOUR,8,DATEADD(DAY,DATEDIFF(DAY,0,@StartDate),2))
IF DATEPART(WEEKDAY,@EndDate) = 1
SET @EndDate = DATEADD(HOUR,19,DATEADD(DAY,DATEDIFF(DAY,0,@EndDate),-2))
IF DATEPART(WEEKDAY,@EndDate) = 7
SET @EndDate = DATEADD(HOUR,19,DATEADD(DAY,DATEDIFF(DAY,0,@EndDate),-1))
IF DATEPART(WEEKDAY,@StartDate) = 2 AND DATEPART(HH, @StartDate)<8
SET @StartDate = DATEADD(HOUR, 8, DATEDIFF(DAY, 0, @StartDate))
IF DATEPART(WEEKDAY,@EndDate) = 6 AND DATEPART(HH, @EndDate)>8
SET @EndDate = DATEADD(HOUR, 19, DATEDIFF(DAY, 0, @EndDate))
Declare @WorkMin int
Set @WorkMin=DATEDIFF(MI,@StartDate,@EndDate)-(DATEDIFF(WEEK,@StartDate,@EndDate)*2880)
Print @WorkMin ----Out put is 2705
---------Actual Minutes is 1985
上面的代碼是計算分鐘週五7之前AM.But我要排除那些minutes.So任何一個可以幫助我在這?
SQL-Server或mysql或Oracle DB?標籤說,MySQL或Qoracle DB和你的問題要求爲SQL服務器 – Jens
只需要爲SQL服務器 – Malyadri
不是所有的國家都有相同的假期 –