How to find Last Date and First Date of Current Month in T-SQL Code?

How to find Last Date and First Date of Current Month in T-SQL Code?

declare @startDate datetime
declare @EndDate datetime
declare @currentDate datetime
set @currentDate = getdate()

set @startDate = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@currentDate)-1),@currentDate),101) -- 1st day of current month
set @EndDate = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@currentDate))),DATEADD(mm,1,@currentDate)),101) -- last day of current month

declare @startDateTwoYearsBack datetime
set @startDateTwoYearsBack = DATEADD(yy,-2,@startDate) -- for getting date which is 2 years back.

select @startDate, @EndDate,@startDateTwoYearsBack

No comments: