3

I have an @StartDate and @EndDate.

I need the @StartDate to be the day the query is ran(which will always be the first of the month) and the @EndDate to be exaclty at the end of the month no matter if the month is 30 or 31 days, etc.

user380432
  • 4,619
  • 16
  • 51
  • 62

1 Answers1

6

A worked example:

DECLARE @StartDate DATETIME, @EndDate DATETIME

SET @StartDate = '2010-01-01 00:00:00.000'
SET @EndDate = DATEADD(m, 1, @StartDate)

SELECT @StartDate, @EndDate - 1

Basically you want to take the start date, add one month (that's what the DATEADD is doing), and then deduct one day.

The output from that query is:

StartOfMonth            EndOfMonth
----------------------- -----------------------
2010-01-01 00:00:00.000 2010-01-31 00:00:00.000
Rob
  • 45,296
  • 24
  • 122
  • 150
  • If you wanted to use current date. SET @StartDate = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) – etliens Aug 26 '10 at 17:34