2014-02-27 31 views
2

我已經讓支付表中的4條記錄說。像這樣在SQL服務器中的所述日期之間的日期與空

UserID Pay startdate  enddate 

     44  10  2006-12-03 2007-12-31    
     44  11  2008-01-01 2008-11-01 
     44  12  2008-11-02 2012-02-05 
     44  13  2012-02-05 NULL 

用戶44 10,11,12,13支付每小時$中提到的幾年。我的問題是,如果現在我給一個日期可以說02/27/2010,那麼它應該帶來12美元作爲他的薪水。基本上檢查開始和結束之間的日期和給。但是現在如果用戶給出像2013年2月27日那樣的日期,那麼它應該給出13美元。

回答

1

嗯,如果只有enddate字段是空的,你可以做一個查詢像

select UserId, Pay 
from Pay 
where @date between startdate and isnull(enddate, '9999-12-31') 
1

你想要一個where聲明:

select pt.pay 
from PayTable pt 
where '2010-02-27' >= startdate and 
     ('2010-02-27' <= enddate or enddate is null); 
+0

@Carth。 。 。是的,那是一個錯字。 –

相關問題