/*------------------------------this week----------------------------------------*/--first day of the weekSELECT DATEADD( Day,1-(DATEPART(Weekday,getdate())+@ @DATEFIRST-1)%7,getdate()) --orSelect DateAdd(WK,DateDiff(WK,0,getdate()),0) --The last day of the weekSelect DateAdd(WK,DateDiff(WK,0,getdate()),6) /*-----------------------------this month, last month, next month-------------------------------------------*/ --first day of last monthSELECT CONVERT(CHAR(Ten),DATEADD(Month,-1,DATEADD(DD,- Day(GETDATE())+1,GETDATE())),111) --last day of last monthSELECT CONVERT(CHAR(Ten),DATEADD(MS,-3,DATEADD(MM,DATEDIFF(MM,0,getdate()),0)),111)+'23:59:59' --first day of the monthSelect DateAdd(DD,-DatePart(DD,getdate())+1,getdate()) --first Monday of the monthSELECT DATEADD(WK,DATEDIFF(WK,"',DATEADD(DD,6 - Day(getdate()),getdate())),"') --last day of the monthSelect DateAdd(DD,-DatePart(DD,getdate()) ,DateAdd(MM,1,getdate())) --days of the monthSelect DateDiff(DD,DateAdd(DD,-DatePart(DD,getdate())+1,getdate()),DateAdd(DD,-DatePart(DD,getdate())+1,DateAdd(MM,1,getdate()))) --orSelect DatePart(DD,DateAdd(DD,-1,DateAdd(MM,1,cast(cast( Year(getdate()) as varchar)+'-'+cast(Month(getdate()) as varchar)+'-01' as datetime)))) --the first day of next monthSelect DateAdd(DD,-DatePart(DD,getdate())+1,DateAdd(MM,1,getdate())) --the last day of next monthSELECT CONVERT(CHAR(Ten),DATEADD(MS,-3,DATEADD(MM,DATEDIFF(M,0,getdate())+2,0)),111)+'23:59:59' /*-----------------------------this season----------------------------------*/ --first day of the quarterSELECT DATEADD(QQ,DATEDIFF(QQ,0,getdate()),0) --last day of the quarter (direct reckoning method)SELECT DATEADD( Day,-1,CONVERT(Char(8),DATEADD(Month,1+DATEPART(Quarter,getdate())*3-Month(getdate()),getdate()), -)+'1') /*-----------------------------this year, last year----------------------------------*/ --last day of last yearSELECT DateAdd(MS,-3,DATEADD(YY,DATEDIFF(YY,0,getdate()),0)) --The first day of this yearSELECT DATEADD(YY,DATEDIFF(YY,0,getdate()),0) --last day of the yearSELECT DateAdd(MS,-3,DATEADD(YY,DATEDIFF(YY,0,getdate())+1,0))
SQL Server gets (this week, this month, Lent, this season, this year) one day