Tuesday, March 20, 2012

Date Calulations

WEEK START DATE and WEEK END DATE

declare @dtDate as datetime
set @dtDate= getDate()
SET DATEFIRST 1
--WEEK START DATE
select convert(char(10),dateadd(week,-1,dateadd(day,-1*(datepart(weekday,@dtDate)-1),@dtDate)),101)
--WEEK END DATE
select convert(char(10),dateadd(day,-1*(datepart(weekday,@dtDate)-1),@dtDate),101)

MONTH START and END DATE

declare @dtDate as datetime
set @dtDate= getDate()
--MONTH START DATE
select convert(char(10),dateadd(month,-1,dateadd(day,-1*datepart(day,@dtDate)+1,@dtDate)),101)
--MONTH END DATE
select convert(char(10),dateadd(day,-1*datepart(day,@dtDate),@dtDate),101)
or
select DATEADD(MM, DATEDIFF(MM, 0, GETDATE()) + 1, 0)-1 AS MonthEndDate



No comments:

Post a Comment