Wednesday, August 30, 2017

Get first date and last date of month and week of current date in sql server

DECLARE @Month int
DECLARE @Year int
DECLARE @Week int
SET @Month = MONTH(GETDATE())
SET @Year = YEAR(GETDATE())
SET @Week = DATEPART( WEEK,GETDATE())


/*First date of month*/

select DATEADD(month,@Month-1,DATEADD(year,@Year-1900,0))

/*Last date of month*/

select DATEADD(day,-1,DATEADD(month,@Month,DATEADD(year,@Year-1900,0)))

/*First date of week*/

select DATEADD(wk,@Week-1,DATEADD(year,@Year-1900,0))

/*Last date of week*/

select DATEADD(day,-1,DATEADD(week,@Week,DATEADD(year,@Year-1900,0)))

No comments: