Sql – GETDATE last month

sqlsql serversql-server-2005

I am trying to list last a website's statistics.
I listed Last 30 days with;

CONVERT(VARCHAR(10), S.DATEENTERED, 101) 
  BETWEEN 
    CONVERT(VARCHAR(10), GETDATE()-30, 101) 
  AND 
    CONVERT(VARCHAR(10), GETDATE(), 101) 

and this month with;

RIGHT(CONVERT(VARCHAR(10), S.DATEENTERED, 103), 7) = 
RIGHT(CONVERT(VARCHAR(10), GETDATE(), 103), 7)

but I have no idea what query to use for last month. I tried with;

RIGHT(CONVERT(VARCHAR(10), S.DATEENTERED, 103), 7) = 
RIGHT(CONVERT(VARCHAR(10), GETDATE()-1, 103), 7) 

Did not work.

Best Answer

Dates are always a joy to work with in any programming language, SQL not excluded.

To answer your question to find all records that occurred last month

select S.DATEENTERED
      ,*
  from sometable S
 where S.DATEENTERED
       between dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())), 0)
           and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(MM, -1, getdate())) + 1, 0))
order by 1

To expand the best means for getting records within a certain time-frame is by utilizing the datediff function, dateadd function, and the between condition in the where clause.

select 'howdy'
      ,getdate()
 where getdate()
       between dateadd(mm, 0, 0)
           and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0))

The above code will result in no records returned because it is checking to see if today's date is between 1900-01-01 00:00:00.000 and the last possible recorded date of last month (the last day and 23:59:59.997 - SQL Server DATETIME columns have at most a 3 millisecond resolution).

The following code will return a record as the date we are searching for is one month ago.

select 'howdy'
      ,dateadd(mm, -1, getdate())
 where dateadd(mm, -1, getdate())
       between dateadd(mm, 0, 0)
           and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0))

A break down of the where clause:

WHERE getdate()  -- date to check
between dateadd(mm, 0, 0) -- begin date
and dateadd(ms, -3, dateadd(mm, datediff(mm, 0, dateadd(mm,-1,getutcdate())) + 1, 0)) -- end date

Finally, a variety of dates can be ascertained in this manner here is a pretty complete list:

select dateadd(mm, 0, 0) as BeginningOfTime
      ,dateadd(dd, datediff(dd, 0, getdate()), 0) as Today
      ,dateadd(wk, datediff(wk, 0, getdate()), 0) as ThisWeekStart
      ,dateadd(mm, datediff(mm, 0, getdate()), 0) as ThisMonthStart
      ,dateadd(qq, datediff(qq, 0, getdate()), 0) as ThisQuarterStart
      ,dateadd(yy, datediff(yy, 0, getdate()), 0) as ThisYearStart
      ,dateadd(dd, datediff(dd, 0, getdate()) + 1, 0) as Tomorrow
      ,dateadd(wk, datediff(wk, 0, getdate()) + 1, 0) as NextWeekStart
      ,dateadd(mm, datediff(mm, 0, getdate()) + 1, 0) as NextMonthStart
      ,dateadd(qq, datediff(qq, 0, getdate()) + 1, 0) as NextQuarterStart
      ,dateadd(yy, datediff(yy, 0, getdate()) + 1, 0) as NextYearStart
      ,dateadd(ms, -3, dateadd(dd, datediff(dd, 0, getdate()) + 1, 0)) as TodayEnd
      ,dateadd(ms, -3, dateadd(wk, datediff(wk, 0, getdate()) + 1, 0)) as ThisWeekEnd
      ,dateadd(ms, -3, dateadd(mm, datediff(mm, 0, getdate()) + 1, 0)) as ThisMonthEnd
      ,dateadd(ms, -3, dateadd(qq, datediff(qq, 0, getdate()) + 1, 0)) as ThisQuarterEnd
      ,dateadd(ms, -3, dateadd(yy, datediff(yy, 0, getdate()) + 1, 0)) as ThisYearEnd

Using the above list a range of any type can be determined.