Searching for dates in a table

Results 1 to 2 of 2

Thread: Searching for dates in a table

  1. #1
    Join Date
    Dec 1969

    Default Searching for dates in a table

    Hey<BR><BR>I have a table with a list of matches all of a specific date (e.g 17/02/04) and I was wondering if it was possible to set up a query so that it found the 2 matches closest to today&#039;s date e.g the closest one before today and the closest one after today and then returned that to put on my front page of my website.<BR><BR>I seem to remember something about talking away 2 dates to find the difference but wondered if there was an easier way of doing it. And would I have to use 2 queries or one?<BR><BR>Cheers (hope that makes sense!)<BR><BR>Mark

  2. #2
    Join Date
    Dec 1969

    Default In MS Access you can use the...

    ...DateDiff() function to work out how many days (or hours or whatever) the dates are apart from each other. However, you will get negative and positive numbers, so simply put an Abs() around the whole thing to get the absolute number:<BR><BR>SELECT TOP 2 * FROM MyTable ORDER BY Abs(DateDiff("d", DateSerial(2004, 2, 17), MyDate));<BR><BR>Selects the top 2 records where the difference between the date in the record and 17/02/2004.<BR><BR>Oliver.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts