2015-11-18 50 views
0

我在StartDate前兩天和EndDate後兩天之間創建一個查詢來獲得員工。它適用於週二和週四的StartDate。如果這些日子在星期五至星期一下班,它將無法正常工作。我不想在星期六和星期日計算。在SQL Server中計算營業日

這裏是我的查詢:

select 
    FirstName, 
    LastName, 
    StartDate, 
    EndDate 
from 
    Students 
where 
    StartDate > GetDate()-2 
    AND EndDate < GetDate() +2 

我如何改變它只能算工作日?

非常感謝!

+0

潛在的相關:http://stackoverflow.com/questions/1803987/how-do-i-exclude-weekend-days-in-a-sql-server-query – AHiggins

+0

如果'GETDATE()'是一個星期五,你想看看下週一或週二結束的學生嗎? – AHiggins

+0

呃,我沒有想過這個。我的主要觀點是列出所有StartDate從今天開始不到2天以及今天過後2天的所有學生,而不提及週末(週六和週日)。謝謝。 –

回答

0

如果我理解正確,您的開始日期不能在週末。要排除週末開始的學生,請添加到where子句AND datepart(dw, StartDate) not in (1,7)

select 
    FirstName, 
    LastName, 
    StartDate, 
    EndDate 
from 
    Students 
where 
    StartDate > GetDate()-2 
    AND EndDate < GetDate() +2 
    AND datepart(dw, StartDate) not in (1,7) 

希望這會有所幫助。一定要檢查向上箭頭如果這個答案對你有用!