Compare dates in Access SQL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobinDiederen
    New Member
    • Sep 2006
    • 19

    Compare dates in Access SQL?

    Suppose, I want to compare 2 dates in an SQL query, running from Access (from VB code) like this:

    "SELECT * FROM Plan WHERE Date1 > Date2"

    Will that work? Thanks in advance!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by RobinDiederen
    Suppose, I want to compare 2 dates in an SQL query, running from Access (from VB code) like this:
    "SELECT * FROM Plan WHERE Date1 > Date2"
    Will that work? Thanks in advance!
    That should work just fine. They are fields of the type "Date/Time", correct?

    Comment

    • sashi
      Recognized Expert Top Contributor
      • Jun 2006
      • 1749

      #3
      Originally posted by RobinDiederen
      Suppose, I want to compare 2 dates in an SQL query, running from Access (from VB code) like this:

      "SELECT * FROM Plan WHERE Date1 > Date2"

      Will that work? Thanks in advance!
      Hi there,

      Killer was correct, you may want to try the below code segment too, hope it helps. Good luck & Take care.

      Code:
        "SELECT * FROM Plan WHERE #Date1# > #Date2#"

      Comment

      • RobinDiederen
        New Member
        • Sep 2006
        • 19

        #4
        Originally posted by Killer42
        That should work just fine. They are fields of the type "Date/Time", correct?
        Exactly.. gonna try that!

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by sashi
          Killer was correct, you may want to try the below code segment too, hope it helps. Good luck & Take care.
          Code:
            "SELECT * FROM Plan WHERE #Date1# > #Date2#"
          Good point. I was assuming that Date1 and Date2 actually both referred to database fields. To specify a date literal, you need to surround it with hashes (#) the same way you do with quotes around a string. To illustrate...
          Code:
          SELECT * FROM Plan WHERE TextField = 'Some String'
          SELECT * FROM Plan WHERE DateField > #11/30/2006#

          Comment

          • atsukoarai86
            New Member
            • Mar 2008
            • 6

            #6
            Originally posted by sashi
            Hi there,

            Killer was correct, you may want to try the below code segment too, hope it helps. Good luck & Take care.

            Code:
              "SELECT * FROM Plan WHERE #Date1# > #Date2#"
            lol this post actually helps me too. I forgot...well, actually I didn't know that you needed to put the date in # #s ....so my WHERE statement wasn't executing and I couldn't understand why!!!

            boy I love forums. ^_^ Thank you.

            Comment

            • mikkaro04
              New Member
              • Oct 2009
              • 3

              #7
              Follow Up question

              hi.. i have a problem almost the same as the one posted so i decided to post it as a reply.. hope someone could help me..
              i want to compare dates from access to a date from DTpicker control in VB 6.0.. this is my code but it doesnt work coz i get a syntax error..

              "Select * from Reservations where DateCheckIn <= #date1# and DateCheckOut > #date1# and [RoomNo] = ' " & cmb_room & " ' "

              "Select * from Reservations where #DateCheckIn# <= #date1# and #DateCheckOut# > #date1# and [RoomNo] = ' " & cmb_room & " ' "

              notes:
              DateCheckIn and DateCheckOut are access fields with Data Type "Date/Time"
              date1 and date2 are Values from my two different DTpickers.
              Reservations is my table name

              both codes give me a syntax error.. can anyone please suggest a better code?

              thnx a lot...

              Comment

              • smartchap
                New Member
                • Dec 2007
                • 236

                #8
                Please see other post where u posted same question. Plz visit the following link for correction of query:
                vbCity is a community of VB and .NET developers joined together with a common goal: to learn, teach, and have fun programming. Developers from all over the world come together to share knowledge, source code, and tutorials for free to help their fellow programmers - Professional Developers, Hobbyists and Students alike. (http://vbCity.com)


                Correct the following query:

                "Select * from Reservations where DateCheckIn <= #date1# and DateCheckOut > #date1# and [RoomNo] = ' " & cmb_room & " ' "

                As:

                "Select * from Reservations where DateCheckIn <= #" & date1 & "# and DateCheckOut > #" & date1 & "# and [RoomNo] = ' " & cmb_room & " ' "


                Similarly correct query:

                "Select * from Reservations where #DateCheckIn# <= #date1# and #DateCheckOut# > #date1# and [RoomNo] = ' " & cmb_room & " ' "

                As:

                "Select * from Reservations where #DateCheckIn# <= #" & date1 & "# and #DateCheckOut# > #" & date1 & "# and [RoomNo] = ' " & cmb_room & " ' "

                Hope it works perfect.

                Comment

                • Dököll
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 2379

                  #9
                  Originally posted by RobinDiederen
                  Suppose, I want to compare 2 dates in an SQL query, running from Access (from VB code) like this:

                  "SELECT * FROM Plan WHERE Date1 > Date2"

                  Will that work? Thanks in advance!
                  It is so funny you wrote, I am working with dates today-)

                  It looks like it should work. You just need to be careful with database columns as far as dates types, Short Date, Long Date and so on. In which case you should try to go ahead and convert whatever date you are selecting from (a vb form), you will aways be sure you are sending the right dates over.

                  Don't go through what I went through:


                  ...

                  WHERE Convert(varchar (12), YourDate, 101) = '"+YourDate+ "')

                  above should let you search/select using mm/dd/yy format, even though the database date is mm/dd/yyyy hh ss.. this that and the other.

                  Good luck, do stay tuned for a better response if this is not it:-)

                  Comment

                  Working...