I want to get the records of last month based on my db table [member] field "date_created".
What's the sql to do this?
For clarification, last month - 1/8/2009 to 31/8/2009
If today is 3/1/2010, I'll need to get the records of 1/12/2009 to 31/12/2009.
I'm from Oracle env and I would do it like this in Oracle:
Idea: I'm running a scheduled report of previous month (from day 1 to the last day of the month, not windowed). This could be index unfriendly, but Oracle has fast date handling anyways. Is there a similar simple and short way in MS SQL? The answer comparing year and month separately seems silly to Oracle folks.
If you are looking for previous month data:
This will also work when the year changes. It will also work on MySQL.
One way to do it is using the DATEPART function:
will return all dates in april. For last month (ie, previous to current month) you can use GETDATE and DATEADD as well:
SQL query to get record of the present month only