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

x
  • 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()))
0
dkilby
Asked:
dkilby
1 Solution
 
_agx_Commented:
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

0
 
dkilbyAuthor Commented:
thanks for the help
0

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