7
Query criteria 2: Using date criteria in queries j then press F5 or click Slide Show > From Beginning to start the course. In the message bar, click Enable Editing, If the videos in this course don’t play, you may need to download QuickTime or just switch to PowerPoint 2013 .

Microsoft Office Access 2013 Tutorial -Learning Criteria-2

Embed Size (px)

Citation preview

Page 1: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

Query criteria 2: Using date criteria in queries

j

then press F5 or click Slide Show > From Beginning to start the course.

In the message bar, click Enable Editing,

If the videos in this course don’t play, you may need to

download QuickTime or just switch to PowerPoint 2013.

Page 2: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

5 61 2 3 4

Course summary Help

1/3 videos

Query criteria 2: Using date criteria in queries Closed captions

Summary Feedback Help The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17

Press F5 to start, Esc to stop

When you use criteria to filter your query results, date criteria can be kind of tricky.So in this course, I'll show you how to use date criteria in queries, starting with the basics.To filter for date values, you surround them with pound signs, like this.If you don't use pound signs, you won't get a result.And remember, you need to enter the entire value in the field. If you enter part of one, like this, you won't

get a result.

Now, the example you just saw filtered for a single date.If you want more flexibility, you can use the Or operator, like this.You can also use some basic expressions.An expression is a combination of functions, logical operators, constants, and fields.For example, I can use the NOT operator to find everything but assets purchased on April 4th of 2013.I can also use the greater-than sign to find dates after April 4th, like so,or the less-than sign to find all dates before April 4th.Just remember to put your operators outside the pound signs.You can also use the AND operator.For example, this criteria finds all records after the second and before the fourteenth.If you need to include data from the dates listed, use BETWEEN and AND.And I know I've said it before, but remember to surround your date values with pound signs.And while I'm repeating myself, remember that pound signs return entire date values.You can't use them to find part of a date, like this.If you need to filter for part of a date, just years or months, for example, you can create a calculated field,and we'll look at how to do that in the next video.

Page 3: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

5 61 2 3 4

Course summary Help

2/3 videos

Query criteria 2: Using date criteria in queries Closed captions

Summary Feedback Help

Press F5 to start, Esc to stop

The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17

The expressions we saw in the previous video only work on whole date values.For example, this expression returns data for all dates except March 22nd of 2013.But if I only use part of the date value in my expressions, I get no result.So, when you need to filter on part of a date value, you can use a calculated field.Calculated fields don't exist in your data.Instead, they display the results of calculation that you define in a new field in your query.As a first example, I'll filter for assets purchased in a given year.Click the top row of a blank field, enter equals, YEAR—that's the function—and a set of parentheses.In the parentheses, add a pair of brackets, and then Acquired Date, the field with my date values.In the Criteria row, I'll enter 2013, and notice that I don't use quotes or pound signs around the value.You do that because the YEAR function returns a number, so I can just use the number here.I'll also hide the Acquired Date field, that makes the data easier to read,and you can see the criteria filters for assets purchased in 2013.So back to Design view, and notice this: Access gave the field a name and removed the equal sign.That's normal, and if you want to, you can rename the field, another way to make your data easier to

understand.

Also, you need to remember a key piece of syntax.When you use a field name in an expression, and the name has spaces, put it in brackets, like this.Capitalization doesn't matter, but spelling does.As another example, say your boss needs to know what was purchased during the second quarter of 2013.For this I'll use the DatePart function, because it lets me filter on time intervals—months, and so on.The function uses any of ten interval settings, from years down to seconds, and here they are.And don't try to memorize these now. The course summary has links to this information.The function also requires a date, either a specific date, or a list of dates in a field.So after DatePart, I enter “q” because I want to filter by quarter, and make sure you use quotes around this

setting.

Add a comma after the quotes, and then Acquired Datebecause that's the field where the date values are, and don't forget the brackets.Now enter 2 in the Criteria row, because you want results for the second quarter, and run the query.Finally, you can do a lot more with expressions than what you've seen here,so the course summary has links to more information.Next, we'll see how to calculate intervals between dates with the DateDiff function,and how to add or subtract dates with the DateAdd function.

Page 4: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

5 61 2 3 4

Course summary Help

3/3 videos

Query criteria 2: Using date criteria in queries Closed captions

Summary Feedback Help

Press F5 to start, Esc to stop

The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17

Let's look at how to use the DateDiff and DateAdd functions in query criteria, and we'll start with DateDiff.DateDiff calculates the number of time intervals, such as days or months, between two Date/Time values.The function requires an interval setting, followed by a start date and an ending date.Here's a list of the interval settings, and don't try to memorize them now. The course summary has a link to

them.

So as an example, here's a query that returns order data—when an order was placed, when it was due, and

when it was shipped.

Now let's say you need to know which orders were late, and by how many days.Go to Design View, expand a blank field, enter a field name, a colon, DateDiff, and parentheses.I want to know how many days an order was late, so I enter “d”, the day interval, with the quotes, and a

comma after.

For my starting and ending dates, I'll enter brackets, with a comma between them, then the field names.That way, I don't forget the brackets.Now let's stop and go back to the unfiltered data for a moment.You can see that not all of the orders are late. Some are early, and some are on time.So if I run my query as it is now, I get a lot of negative values, and those numbers point to orders that

shipped on time.

Because I only want to see late orders, I enter a greater-than sign and zero in the Criteria row,and sort the results in Descending order.Run the query, and there you go.You use the DateAdd function when you need to add date values together.DateAdd works a lot like DateDiff, but it uses slightly different syntax.You start with an interval, but then you specify a number, and this is the number of intervals you want to

add.

So if I specify the year interval, then 3, I'm adding 3 years' worth of values.You follow that with a date, and this is the date you'll add your values to.So in this example, I'm filtering for three years' worth of data, starting with October 16th of 2015.To show you how this works, I'll use the Criteria row instead of a calculated field,and I'll filter for records from the past four years.Start with the Between operator, then the Date function. That gives me a date automatically, so I don't have to enter one each time I run this query.It saves me some typing.Follow that with the AND operator, then DateAdd, parentheses,the year interval, negative four, and then the Date function again.The second date function is there because DateAdd has to add your values to something,and the Date function provides that something—today's date.You can also use specific dates, like this, but make sure you surround them with quotes.Run the query, and I get earlier dates, records from the past four years.Change the interval number to a positive number, and I get later values.Now, that's just one example of how to use the DateAdd function, so see the Course Summary for links to

more information.

Page 5: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

Help Course summary

Press F5 to start, Esc to stop

Course summary—Query criteria 2: Using date criteria in queries

Summary Feedback Help

5 61 2 3 4

The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17

See also• Using dates as criteria in Access queries

• Build an expression

• Choose the right date function

• DatePart, DateDiff, & DateAdd

• More training courses

• Office Compatibility Pack

The basicsSurround your date values with pound signs (#). For

example, #4/1/1997#. Also, keep logical operators

outside pound signs.

• #4/1/1997# Or #5/4/1997#

• >=#5/4/1997# Or <=#6/12/1998#

Filter for a given year=Year([Acquired Date]), then put the year you want to

filter for in the Criteria row.

Filter for an interval (year,

quarter, etc.)=DatePart("q",[acquired date]). In this example, put the

numbers 1-4 (the quarter you want to filter for) in the

Criteria row.

Subtract dates=DateDiff("d",[RequiredDate],[ShippedDate]). Finds the

days between a required date and a shipped date.

Add dates=DateAdd("m",6,"4/1/1997") Finds 6 months of data,

starting with the date provided.

DatePart, DateDiff, & DateAdd

interval settingsyyyy

q

m

y

d

w

ww

h

n

s

Year

Quarter

Month

Day of year

Day

Weekday

Week

Hour

Minute

Seconds

Page 6: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

Check out more courses

Help Course summary

Press F5 to start, Esc to stop

Rating and comments

Thank you for viewing this course!

Please tell us what you think

Summary Feedback Help

5 61 2 3 4

The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17

Page 7: Microsoft Office Access 2013 Tutorial -Learning Criteria-2

Help Course summary

Press F5 to start, Esc to stop

Help

Summary Feedback Help

5 61 2 3 4

Using PowerPoint’s video controls

Going places

Stopping a course

If you download a course and the videos don’t play

get the PowerPoint Viewer. the QuickTime

player upgrade to PowerPoint 2013

The basics Calculated fields DateDiff, DateAdd

4:182:30 3:17