Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another 'Get last month's data' question

Status
Not open for further replies.

elsenorjose

Technical User
Oct 29, 2003
684
US
I'm using the following syntax to retrieve data for the period between Sunday and Saturday of the prior week regardless of what day I run this query in the current week.

...WHERE YEARweek(tblDate) = YEARweek(CURRENT_DATE - INTERVAL 7 DAY)

What is the corresponding syntax to retrieve records between the first and last day of the prior month, regardless of when in the current month I run this?

Thanks in advance.
 
Code:
where tblDate 
       >= date_sub(
          date_sub(current_date
              , interval dayofmonth(current_date)-1 day)
              , interval 1 month)
  and tblDate 
        < date_sub(current_date
              , interval dayofmonth(current_date)-1 day)

r937.com | rudy.ca
 
Wow...
Whole lot easier to do it for a week, eh?
Thanks!
 
well, i suppose

however, if you apply a function to a column, then mysql cannot use an index on that column, and so has to do a table scan

won't matter until you get large volume, at which point the query will get slower and slower...

r937.com | rudy.ca
 
This function will be used to create a view. The table is already very large so I'm summarizing by week and month ranges.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top