• What is TechTraction?

    Personal commentary on technology with a sprinkling of tech-tips and how-to articles

MySQL Tip: Retrieve Table Data a Specific Number of Days Back in Time

Hard Drive StorageI’ve been working on a project that required some date based MySQL queries. I’m not sure what it is, but I always find date based queries a little tricky. Seems that the data I need is never in the format I want or I’m always looking for data from a few days back and need to do some “date math” to get there. Unfortunately, I don’t work with MySQL everyday, so the necessary functions and data type restrictions to retrieve what I need aren’t in the forefront of my memory. Keeping that in mind, I thought I would post a quick MySQL tech-tip for anyone else that occasionally struggles with date centric MySQL queries.

MySQL query requirement

Here is what I need to do with my custom MySQL query: View all of the rows of data entered into a specific table 2 days ago. The starting point for 2 days back is always based on the current date. For example, if today is 1/16/2008, then I’m looking for data from 1/14/2008.

Data restrictions for the requirement

I’m working from a 10 column table that includes a timestamp column. This column is of type Date Time so I’ll need to leverage this column to extract the data from 2 days ago; however, since the column is of type Date Time it contains the date and the time for a particular data row. Also, I’m interested in all of the data from 2 days ago regardless of the time for a particular row of data. Therefore I’ll need to take that into consideration as I write my query.

MySQL functions needed for date manipulation

To start, I need a way to determine the current date. Thankfully, MySQL has a very simple and intuitive function called CURDATE(); however, CURRENT_DATE and CURRENT_DATE() are synonyms for CURDATE() and work exactly the same. And while CURDATE() is shorter and quicker to type, I plan to use CURRENT_DATE() for future readability.

Next, I need a way to do some “date math” so I can figure out the date for 2 days ago based on today. For that I’ll use DATE_SUB(). This function can also be called SUBDATE(), but again, for future readability I’ll use DATE_SUB() which takes two arguments: the date from which to start the calculations and the interval expression unit.

For example, if I wanted to calculate the date from 2 days ago from today (1/16/2008), then I’d type the following:

DATE_SUB('1/16/2008', INTERVAL 2 DAY)

Likewise, if I wanted to determine what the date was 2 months ago from today, then I’d type the following:

DATE_SUB('1/16/2008', INTERVAL 2 MONTH)

However, since my stating date is always based on today, then I’ll call this function in the following manner:

DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY)

Extract the date from a column containing date and time

Finally, since I’m trying to match the data stored in my timestamp column, I need to extract just the date portion of the value stored. Because if I try to match just a “date” against “date and time,” my query won’t return any results. Therefore I’ll need to use the DATE() function in the following manner to extract just the date from this column and then perform my match:

DATE(timestamp)

Now, using the DATE(), DATE_SUB(), and CURRENT_DATE() functions I can view all of the transactions in my PURCHASES table from 2 days ago.

SELECT * FROM purchases WHERE DATE(timestamp) = DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY);

Date based queries are easier than you think

Queries that require some form of date manipulation are always a little strange (at least they are for me). Fortunately, MySQL offers several built in functions to make these types of queries much easier. For a complete list of the available functions along with further explanation on the allowed arguments (such as the ones allowed for DATE_SUB()), check out the MySQL online function reference and the MySQL date time type definitions.


If you liked this post, subscribe to TechTraction's RSS feed or TechTraction's email feed

Filed under: How-To & Tech Tips

6 Responses to “MySQL Tip: Retrieve Table Data a Specific Number of Days Back in Time”

  1. This is great… exactly what I needed. Thanks!

  2. Glad you found the post useful. I know exactly how it feels to be searching for an answer and to find exactly what you were looking for. Glad I could help.

  3. This is also exactly what I need, thanks for taking the time to post it - I appreciate it!

  4. Thanks for the comment Jeremy and I’m glad you found this post useful.

  5. hi this is very good but here u find data from 2 days back.I want to ask that can we get data which has start time and end time and when current date is greater than end date the data should not be displayed. can u help it?

  6. Yes Kalpesh, you can retrieve data older than the current date and within a time range provided you have the time easily accessible. If you can provide a brief summary of the data and their types I could provide a better example of how to do this.

Leave a Reply