Share via

Time criteria on a Date/Time field in a query

Anonymous
2015-04-07T23:20:51+00:00

How do I use time criteria on a Date/Time field in a query?

The records in my table represent events and the table has a Date/Time field that contains both the date and time in the same field.  If I want to return a list of events that occurred between 9pm and 11pm (the date does not matter, only the time matters), how do I enter that criteria in the query grid?  I tried entering Between #9:00:00 PM# And #11:00:00 PM# but that returned no records even though there are several events within those times.

The Microsoft support website has plenty of examples of using date criteria on a Date/Time field, but NOT ONE example of how to use a time criteria on that field.

https://support.office.com/en-US/article/Examples-of-query-criteria-3197228C-8684-4552-AC03-ABA746FB29D8

https://support.office.com/en-us/article/Examples-of-using-dates-as-criteria-in-Access-queries-aea83b3b-46eb-43dd-8689-5fc961f21762?ui=en-US&rs=en-US&ad=US

I've searched many Access books and websites and they all do the same thing.  They talk about the Date/Time field and give plenty of great examples of using date criteria in a query, but after HOURS of searching, I can't find one example of using time criteria!   grrrrrrrrrr!  Also, I've tried everything I could think of to make this work but the query always returns no records.  I'm starting to think it can't be done and that I have to first extract the time from the Date/Time field into separate fields and then query those fields, and if that's the case, wouldn't that make time storage in a Date/Time field pretty worthless?

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2015-04-08T01:34:26+00:00

    You do need to extract the time portion - but you can do it dynamically in the Query without altering the table structure.

    Create a Query based on the table. In a vacant Field cell type

    JustTheTime: TimeValue([datetimefield])

    If the field contains #4/7/2015 11:30am# this calculated field will contain just #11:30am# (well, actually, it will be #12/30/1899 11:30:00am# but since that date is the zero point for date/times, the query will see it as just a time).

    Was this answer helpful?

    10 people found this answer helpful.
    0 comments No comments
  2. ScottGem 68,810 Reputation points Volunteer Moderator
    2015-04-08T12:04:18+00:00

    Actually its not strange at all. First, you have to understand that Access stores Date/Time values as a Double precision number where the integer portion is the number of days since 12/30/1899 and the decimal portion is a fraction of a day (so 6AM is .25)

    So if you enter criteria as Between #9:00:00 PM# And #11:00:00 PM#, it will look for records with a 0 date (i.e. 12/30/1899). Similarly, if you filter by date and use BETWEEN #4/1/15 AND #4/30/15# it will not include records for 4/30/15 9:00 AM. Because the criteria is assuming no decimal value, so its ignoring everything after 4/3/15 00:00AM.

    When you work with Pivot Tables, Crosstab queries or Reports, the wizard to create those items ask what interval you organize the data on. So the wizards will, behind the scenes< parse out the time value if that's what you select. In other words, Access does the heavy lifting for you. 

    Once you understand how datetime values work and how Access does a lot of the work for you, it really hits how powerful Access is. If you you tried to write a C# program to do the same thing, it would take a LOT of coding.

    Was this answer helpful?

    4 people found this answer helpful.
    0 comments No comments
  3. ScottGem 68,810 Reputation points Volunteer Moderator
    2015-04-09T12:34:36+00:00

    The reason for the special Datatype IS so that all database functions will work.

    You don't HAVE to separate out the time unless you want to treat the time independently. For example, if you wanted to find all morning appts irrespective of date.

    But if you want to find all appts for the morning of 4/1 you just have to include the date along with the time.

    Was this answer helpful?

    3 people found this answer helpful.
    0 comments No comments
  4. Anonymous
    2015-04-08T07:49:27+00:00

    Thanks for the reply.  That's strange that I should have to extract the time of a Date/Time field before I can use it as a criteria in a query, because a PivotTable will operate on it just fine without having to extract it.  For example, if I want to view an hourly count of events, all I have to do in PivotTable view is drop the Date/Time field in the Row area, right click on that area and select Properties, Filter and Group tab, and then group items by hour.  Then I can drop any other field in the Data area and boom, I have an hourly count of each event.  (this is one reason why I don't "upgrade" to Access 2013, hahahahaha)

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  5. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more