Results 1 to 8 of 8
  1. #1
    GaryElwood is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    12

    Getting Just the Date part of Date/Time field

    I am trying to count the number of records in a table the have the current date. The date field (DateAdded) is stored as a Date/Time value. So id i use Date() in my query criteria i will never get a match.
    Question: How, in a query, do i compare just the date part of a date/time field to my query criteria?
    Thanks



    Field: Expr1: Format([DateAdded],"mm/dd/yyyy")
    Total: Count
    Criteria: Date()

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Try something like this:

    Code:
     
    SELECT Count ([TableName].[TimestampFieldName]) As TotalRecordsToday
    FROM TableName
    WHERE (((Format([TableName].[TimestampFieldName],"mm/dd/yyyy"))=Date()));
    Hope this helps!

  3. #3
    GaryElwood is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    12
    I tryed your code but got a count of 0. I have one record with todays date?

    Query code:
    SELECT Count ([DataChanges_Part2].[DateAdded]) As TotalRecordsToday
    FROM DataChanges_Part2
    WHERE (((Format([DataChanges_Part2].[DateAdded],"mm/dd/yyyy"))=Date()));

    Multipe records in table [DataChanges_Part2] one with todays date.
    9/28/2011 8:41:19 AM

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Your date is showing up as 9/28/2011?

    In the test I set up the date is defaulting to 09/28/2011.
    And since my format is mm . . . it is matching the way the dates exist in my table.

    Is your Date field defined as a Date/Time field in the Table?

  5. #5
    GaryElwood is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    12
    Yes. The Data Type is set to Date/Time and the format is set to "short Date"

  6. #6
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Just for now - try this [I changed the Format to "m/dd/yyyy"]:
    Code:
     
    SELECT Count ([DataChanges_Part2].[DateAdded]) As TotalRecordsToday
    FROM DataChanges_Part2
    WHERE (((Format([DataChanges_Part2].[DateAdded],"m/dd/yyyy"))=Date()));
    See if that gives you your count.

    Then - see if you can remove your 'short date' format in your table - leave it blank - so that it reverts to the default date format [which I think should be mm/dd/yyyy].

    You need all your dates to have a standard appearance [ie - 09/09/2011 . . . two digits for month & date & 4 digits for year . . .]

    Otherwise that Where clause will not work unless your month is 10 or more and your date is the 10th of the month or more . . . .

    Let me know.

  7. #7
    GaryElwood is offline Novice
    Windows XP Access 2007
    Join Date
    Sep 2011
    Posts
    12
    Thanks!
    Everything seems to be working.
    Thanks again for the help....

  8. #8
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    What did you do?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Date/Time Field Query...
    By Coffee in forum Queries
    Replies: 5
    Last Post: 07-05-2011, 11:27 PM
  2. Access 2007 Date/Time Field
    By cdh in forum Access
    Replies: 3
    Last Post: 05-12-2011, 07:11 AM
  3. Access Subquery Max Date/Time field
    By ruci1225 in forum Access
    Replies: 1
    Last Post: 04-08-2011, 07:33 AM
  4. Replies: 6
    Last Post: 01-04-2011, 05:43 PM
  5. date/time field
    By James890 in forum Database Design
    Replies: 1
    Last Post: 04-18-2007, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums