Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we know how to tell Access to return today's date and time, we can use that information to help obtain query results that are relative to today. We have two different functions that will return slightly different results depending on the type of question that we're trying to ask. The DateAdd function will allow us to specify a time frame such as the past 30 days or within the last year and return records within that timeframe. The DateDiff function will allow us to find how many days have elapsed between today and a specific event. We'll take a look at how we can apply each in our queries with some real world examples.
First let's take a look at the DateAdd function. We'll go to the Create menu and in Query Design and we'll add our table Orders. Go ahead and say Close to the Show Table window and in the first field let's just put in the Field for today's date, date(). Now let's add a DateAdd function. In the next field we'll right-click and say Build. We'll go into the Functions, the Built- In Functions, and the Date/Time category.
Let's scroll until we find DateAdd. We'll double-click on it to add it to our function. So DateAdd requires three parts. The first is an interval. This is if they were looking for months or days or years. The second part is a number. This is how many months or days or years to add or subtract. Finally we need the date that we want to add or subtract from. So if we wanted to find a date that was 24 months ago, we would type it like this. The interval is months, so we'll use the m character.
The number that we want to add is -24. We want 24 months ago. The date that we want to add or subtract from is today. That will use the date function to get. So our finished function reads DateAdd("m", -24, date(), and a final closing parenthesis to finish the function. Okay, let's go ahead and say OK and run our query.
We'll say that Access returns today's date and the same day two years ago or 24 months ago. Let's go ahead and go back into Design view and change it up. I'll expand this open and now once it's created we can just edit it right here. Instead of 24 months ago, let's say one day in the future. We'll change it to a d to indicate that we want to add days instead of months. We'll change the number to a 1 so we'll add one day to today's date. Let's go ahead and run that and we'll see that we get today and tomorrow.
Let's take a look at another one. Let's go back into Design view. This time we're going to add years. Now years is going to use four y's, similar to the Format function symbols. Let's add 3 years to today's date. We'll change that 1 to a 3 and then we'll run our query. Now I've got today's date and a date three years from now. So now that we know how to use today's date and we know how to specify the date in the future or in the past relative to today, we can use these two to define a range.
Let's go back into Design view. Let's use these two fields to create a function that tells us what employees were hired in the last two years. Let's go ahead and delete the Orders table and we'll delete these two fields to get us back to a blank query. We'll Show Table and we'll add our Employees table. Go ahead and say Close. To our query we're going to add FirstName, LastName, and we'll scroll down until we get to HireDate and we'll add that as well.
For HireDate we'll specify a criteria that selects everything more recent than two years ago. Let's expand this field open. In our Criteria we'll say it's greater than the dateadd function, the units or the interval is going to be in years, and the number is going to be -2. We want a date that's two years ago. And finally the day that we want to add or subtract that to is today, date().
Finally let's finish our function with a finishing closing parenthesis. So the finished criteria reads that we want to select all of our dates that are greater than a day two years in the past. Let's go ahead and run this query and we'll see that we have 20 employees they were hired within the last two years. So this is an example of how we can use DateAdd to select a range of dates. Let's find out exactly how many days ago some of these employees were hired. Let's go back into our Design view and we'll use the DateDiff function. In our fourth column we'll right-click and go to our Builder and let's go find the DateDiff function.
We'll look in Built-In Functions, Date/Time and DateDiff. DateDiff has several arguments that we can supply. The first one is interval. This is going to be the same as with DateAdd, whether we're looking at days, months, or years. The second and third arguments are the two dates that we want to find the difference between. The firstdayofweek and the firstweekofyear fields are optional. We can see that if we look at the hints down here in the bottom. They're wrapped in square brackets which is Access's way of telling you that these are optional arguments. We would use these if we needed to redefine when our week started or when our year started. For instance, if we are on a fiscal year that doesn't coincide with the calendar year.
We can go ahead and remove these. We're not going to be using them. But we do need to supply though is the interval and in the two dates that we want to calculate the difference between. So the interval for our example is going to be months. We want to find out how many months has it been since our employees were hired. We'll wrap that in quotation marks so that we maintain proper syntax. Then we need to specify the two dates. The first date that we want to use is the HireDate. So we'll go into our database, we'll go into our Tables, we'll go into our Employees table, and we'll double-click on HireDate.
The date we want to find the difference between is today so we'll use our code to specify today's date, date(). Let's go ahead and say OK and we'll run this query. Now we had a query that's showing us all of the employees they were hired within the last two years and in this fourth column we're seeing exactly how many months ago from the date of this recording that they were hired. Working with dates is an integral part of making sense of all the data within your database.
With a little bit of practice and persistence, mastering date and time-based data and using it to filter out all the relevant records will help you make your queries that much more successful.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 99211 Viewers
80 Video lessons · 141815 Viewers
59 Video lessons · 60194 Viewers
52 Video lessons · 73313 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.