SQLServer日期格式化及创建相关日期
DECLARE @FirstDay_M DATETIME --本月初日期 SET @FirstDay_M = DATEADD(mm,DATEDIFF(mm,0,@bDate),0); DECLARE @LastDay_M DATETIME --本月末日期 SET @LastDay_M = DATEADD(dd,-1,DATEADD(mm,1,@FirstDay_M)) DECLARE @FirstDay_MLY DATETIME --去年本月初日期 SET @FirstDay_MLY = DATEADD(mm,DATEDIFF(mm,0,@bDate)-12,0); DECLARE @LastDay_MLY DATETIME --去年本月末日期 SET @LastDay_MLY = DATEADD(dd,-1,DATEADD(mm,1,@FirstDay_MLY)); DECLARE @FirstDay_Y DATETIME --本年初日期 SET @FirstDay_Y = DATEADD(yy,DATEDIFF(yy,0,@bDate),0); DECLARE @LastDay_Y DATETIME --本年末日期 SET @LastDay_Y = DATEADD(dd,-1,DATEADD(yy,DATEDIFF(yy,0,@bDate) + 1,0)); DECLARE @FirstDay_LY DATETIME --去年初日期 SET @FirstDay_LY = DATEADD(yy,DATEDIFF(yy,0,@bDate) - 1,0); DECLARE @LastDay_LY DATETIME --去年末日期 SET @LastDay_LY = DATEADD(dd,-1,DATEADD(yy,DATEDIFF(yy,0,@bDate),0)); DECLARE @bDate_Y DATETIME -- 去年本日 SET @bDate_Y = dateadd(year,-1,@bDate); DECLARE @DayLastTime DATETIME -- 日期当前的59分59秒 SET @DayLastTime = dateadd(ss,-1,dateadd(dd,1,@bDate));