在收费管理中经常需要进行时间戳的获取,这就涉及到特定日期的取得,
--获取阶段性日期的之前最后日期和本周期最后日期,
--常用于收费的统计的日期戳
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))