Results 1 to 10 of 10

Thread: comparing dates resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    334

    comparing dates resolved

    i have a table that contains dates and rates, i want to create a query which returns the rate corresponding to todays date or the closest date in the past

    (i know how to say select where date = a certain date but how do i do the above???)

    any ideas????
    Last edited by pame1la; Apr 11th, 2005 at 06:54 AM.

  2. #2
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: comparing dates

    loop through the date values and then compare them to today's date using

    ChosenDate = ?
    TodaysDate = Date
    Days = datediff("d",TodaysDate,ChosenDate)

    then the the Days variable will tell you how many days there are between the dates (0 would be today) so in your app you could find the date with the lowest days value and use that
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    334

    Re: comparing dates

    okay so i am right in thinkin i would have a sql query which will call all the dates and rates from the table, then compare dates with todays date storing the difference in an array. Then go through the array, and find the smallest diff...then get the corresponding date and therefore rate?

    isn't there any similar way of doin it, i mean is there no way of writing a select statement that selects rate where dif between todays date and dte is smallest????

  4. #4
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    Re: comparing dates

    Could you not sort your table into ascending order by date using SORT BY name of date column ASC. Then.. get todays date, search for it in the table then surely the next record (.movenext) will be the closest to todays date?



    BIOSTALL

  5. #5
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: comparing dates

    unfortunately not, the only way to select the smallest value from an array or any collection of values is by looping through all of them
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    334

    Re: comparing dates

    Quote Originally Posted by BIOSTALL
    Could you not sort your table into ascending order by date using SORT BY name of date column ASC. Then.. get todays date, search for it in the table then surely the next record (.movenext) will be the closest to todays date?



    BIOSTALL

    yeah i think so, il try and let u know

  7. #7
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: comparing dates

    @Biostall:

    using a sort would work, however i was under the impression she was doing this entirely from vb, can you sort the columns from there?
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  8. #8
    Addicted Member BIOSTALL's Avatar
    Join Date
    Apr 2005
    Location
    Northampton, UK
    Posts
    180

    Re: comparing dates

    In an sql query you can. And i meant ORDER BY, not SORT BY, sorry

    BIOSTALL

  9. #9
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: comparing dates

    Hold the phone, folks. You can do this with a query like the following:
    VB Code:
    1. strSQL = "SELECT RateField FROM TheTable " _
    2.           & " WHERE DateField = (SELECT MAX(DateField) FROM TheTable " _
    3.           & " WHERE DateField <= #" & Format$(Date, "m/d/yyyy") & "#"
    "It's cold gin time again ..."

    Check out my website here.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    334

    Re: comparing dates resolved

    Thanks.....

Posting Permissions

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



Click Here to Expand Forum to Full Width