Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

ms sql + dates

i would like to get the first day and last of the month of the previous month, i am using the following code, but would like the time for the startdate be 00:00 and end date 23:59 how can i modify to accomplish.

declare @StartDate datetime,
            @EndDate datetime

select @StartDate = DATEADD(dd,-(DAY(DATEADD(mm,1,GetDate()))-1),DATEADD(mm,-1,GetDate()))
select @EndDate = DATEADD(dd, -DAY(DATEADD(m,1,GetDate())), DATEADD(m,0,GetDate()))
1 Solution
Here's one way. There may be a more elegant approach for 2008

declare @StartDate datetime, @EndDate datetime
set @StartDate = dateAdd(m, -1, convert(datetime, convert(varchar(6), getDate(), 112) +'01', 112))
set @endDate = dateAdd(n, -1, dateAdd(m, 1, @startDate))
select @startDate, @endDate

Open in new window

dkilbyAuthor Commented:
thanks for the help

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now