在收費管理中經常需要進行時間戳的獲取,這就涉及到特定日期的取得,
--獲取階段性日期的之前最後日期和本週期最後日期,
--常用於收費的統計的日期戳
DECLARE @chargingPeriod int--週期
SET @chargingPeriod=6
DECLARE @currDay datetime --當前日期
DECLARE @nextMonths INT --與下個日期的月差
DECLARE @lastMonths INT --與上一個日期的月差
SET @currDay='2015-08-02' --假設當前日期
SET @nextMonths=((DATEPART(MONTH, @currDay)-1)/@chargingPeriod+1)*@chargingPeriod-DATEPART(MONTH, @currDay)
SET @lastMonths=(DATEPART(MONTH, @currDay)-1) % @chargingPeriod
--本週期段內的最後一天
SELECT DateAdd(day,-1,DATEADD(mm, DATEDIFF(m,0,@currDay)+@nextMonths+1, 0))
--上個週期段內的最後一天
SELECT DateAdd(day,-1,DATEADD(mm, DATEDIFF(m,0,@currDay)-@lastMonths, 0))