SQL Query Get Data a Month Old
This looks OK to me as long as you want the last 28-31 days or so of data,
including the current day. However, you might consider using
DATEADD/DATEDIFF to remove the time portion of datetime:
DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))
If you want to return only data older than the last 28-31 days, use '<':
WHERE
datecreated <
DATEADD(m,-1,DATEADD(day, 0, DATEDIFF(day, 0, GETDATE()))), GETDATE()
To return only data older than the start of last month:
WHERE
datecreated <
DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 1, 0)
--
Hope this helps.
Dan Guzman
SQL Server MVP
|