Access Database Query Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Catalyst159
    New Member
    • Sep 2007
    • 111

    Access Database Query Help

    I am trying to correct the following query:

    Code:
    UPDATE marriages SET marriages.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
    
    WHERE marriages.[Date of Marriage] Is Not Null OR marriages.[Date of Marriage] <> 'VOID';


    ***What I am trying to do is change this so it will not try and update any records with a [Date of Marriage] value of 'VOID' and also will not try and update any records that have a [Date of Marriage] value of only the year for example '1917' or '1980'.
    ***

    How could I go about changing this query to reflect what I want? I appreciate any help. Thanks.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    First a quick question.
    Is [Date of Marriage] a text field or a date field?
    The example provided seems to suggest a text field, since you can have 'void' in your field.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      How about Updating only if a valid Date exists, as in:
      Code:
      WHERE IsDate(marriages.[Date of Marriage])

      Comment

      • Catalyst159
        New Member
        • Sep 2007
        • 111

        #4
        You are correct. It is a text field.

        Comment

        • Catalyst159
          New Member
          • Sep 2007
          • 111

          #5
          But if a valid date exists then what would it actually be updating?

          Comment

          • Catalyst159
            New Member
            • Sep 2007
            • 111

            #6
            This would update only format then ?

            Comment

            • TheSmileyCoder
              Recognized Expert Moderator Top Contributor
              • Dec 2009
              • 2322

              #7
              I believe so.
              But always take backup before you run a big update query if your not sure on the results :)

              Comment

              • Catalyst159
                New Member
                • Sep 2007
                • 111

                #8
                The problem I am having now is when I run the following query:

                Code:
                UPDATE marriages_cpy SET marriages_cpy.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy')
                WHERE marriages_cpy.[Date of Marriage] <> 'VOID' OR IsDate([Date of Marriage]);

                Any records that have a [Date of Marriage] value of only a year like "1917" or "1980" or any that are not valid dates the values are deleted after running the query. However it does keep the VOID value. I do not want to delete any of the values that are not valid dates. I only want to skip those and not update them.

                Comment

                • TheSmileyCoder
                  Recognized Expert Moderator Top Contributor
                  • Dec 2009
                  • 2322

                  #9
                  You just need to loose the <>Void part, since 1980 for example is different from void, and as such gets updated.
                  Since Void is not a valid date, the IsDate() criteria is enough for your needs.
                  Code:
                  UPDATE marriages_cpy SET marriages_cpy.[Date of Marriage] = Format(CDate([Date of Marriage]),'m/d/yyyy') 
                  WHERE IsDate([Date of Marriage]);

                  Comment

                  • Catalyst159
                    New Member
                    • Sep 2007
                    • 111

                    #10
                    So what would happen to a record that has a value of 1980 then? It would remain 1980 right ?

                    Comment

                    • TheSmileyCoder
                      Recognized Expert Moderator Top Contributor
                      • Dec 2009
                      • 2322

                      #11
                      Yes 1980 would remain 1980. The query will only work on valid dates.

                      Comment

                      • Catalyst159
                        New Member
                        • Sep 2007
                        • 111

                        #12
                        Yes you are right. I just tested it. Looks good. Any ideas as to why it was deleting the value before?

                        Comment

                        • TheSmileyCoder
                          Recognized Expert Moderator Top Contributor
                          • Dec 2009
                          • 2322

                          #13
                          No I dont really see why it would be deleted. What I tried is this:
                          Alt-F11 to open VBE, then Ctrl-G to open/goto immediate pane.
                          Then I typed:
                          Code:
                          ? format("1980","m/d/yyyy")
                          which returned [6-2-1905] (Which is 1980 days after what VB counts as day 0 (12-30-1899)

                          So I could understand if 1980 got replaced by [6-2-1905], but it being deleted makes no real sense to me.

                          Comment

                          • Catalyst159
                            New Member
                            • Sep 2007
                            • 111

                            #14
                            I don't understand why it returns the 6-2-1905. Could you explain this a little more for me. I would appreciate it. Thanks.

                            Comment

                            • TheSmileyCoder
                              Recognized Expert Moderator Top Contributor
                              • Dec 2009
                              • 2322

                              #15
                              When a date gets stored, it gets stored as a double, and each time it gets retrieved, it gets formated back to a date.
                              For example Today is:Cdbl(Date()) =40863
                              where as now() gives: cdbl(Now())=408 63,9424768519
                              the 40863 represents the date, where as .9424768519 represents the time of the day, I presume, as a fraction of a full day, as you can see the day is almost over here :P.

                              From alot of views it makes sense to store the date as a number, because for instance its alot easier to do comparisions on whether or not a date is earlier (smaller) or later (greater) then another date. Im sure there are sites out there that explain it better then me though, so if your still unclear, go look it up. ;P


                              Edit: PS. Its just convention that says that 0 is 30/12/1899.

                              Comment

                              Working...