How do I make months appear to be like 1-12 (first year of my database) and so on?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michelle copper
    New Member
    • Aug 2011
    • 63

    How do I make months appear to be like 1-12 (first year of my database) and so on?

    How do I create a query for a new column for my months to appear like 1-12 means the first year of my database (eg. 2008) and 13 to 24 means (2009) and 2536 (2010) and so on? Is there a way? so it is like 13 means january of 2009 and 14 means february of 2009.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Take the year of the date, subtract 2008, multiply by 12, then add the month number of the date.

    Comment

    • michelle copper
      New Member
      • Aug 2011
      • 63

      #3
      :S
      sorry Rabbit..I did not get what you mean.
      Could you explain? How do I write that in the query?

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32633

        #4
        Something like :
        Code:
        SELECT (Year([YourDate]) - 2008) * 12 + Month([YourDate]) AS [MonthID]

        Comment

        • michelle copper
          New Member
          • Aug 2011
          • 63

          #5
          I do not have DATE in my table actually. I should have mentioned that before
          I have

          ID
          YEAR
          MONTH
          QUARTER
          TIME
          HOME

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            You may also want to make sure that there are no NULL Values in the Date Field, and that it is >=#1/1/2008#.
            1. Sample Data (tblTest)
              Code:
              TestDate
              3 /17/2008
              12/31/2009
              11/30/2009
              3 /3 /2009
              1 /1 /2010
              8 /23/2010
              4 /29/2010
              7 /9 /2010
              12/25/2010
              7 /7 /2008
              3 /3 /2011
              10/10/2012
              6 /16/2012
              5 /5 /2008
              5 /23/2013
              4 /30/2219
              9 /1 /2014
              2 /27/2007
              12/31/2007
              6 /25/2543
            2. SQL Statement:
              Code:
              SELECT tblTest.TestDate, Month([TestDate])+((Year([TestDate])-2008)*12) AS MCode
              FROM tblTest
              WHERE (((tblTest.TestDate)>=#1/1/2008# And (tblTest.TestDate) Is Not Null));
            3. Results:
              Code:
              TestDate	      MCode
              3 /17/2008	      3
              12/31/2009	     24
              11/30/2009	     23
              3 /3 /2009	     15
              1 /1 /2010	     25
              8 /23/2010	     32
              4 /29/2010	     28
              7 /9 /2010	     31
              12/25/2010	     36
              7 /7 /2008	      7
              3 /3 /2011	     39
              10/10/2012	     58
              6 /16/2012	     54
              5 /5 /2008	      5
              5 /23/2013	     65
              4 /30/2219	   2536
              9 /1 /2014	     81
              6 /25/2543	   6426

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Add any additional Fields as needed:
              Code:
              SELECT tblTest.Month, tblTest.Year, ([Month]+([Year]-2008)*12) AS MCode
              FROM tblTest
              WHERE (((tblTest.Year)>=2008));

              Comment

              • NeoPa
                Recognized Expert Moderator MVP
                • Oct 2006
                • 32633

                #8
                Originally posted by Michelle
                Michelle:
                I should have mentioned that before
                Yup. That would make sense ;-)

                Actually it makes it somewhat easier :
                Code:
                SELECT ([YEAR] - 2008) * 12 + [MONTH]) AS [MonthID]

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  The algorithm still stands. In place of the year and month functions, you use your year and month fields.

                  Comment

                  • michelle copper
                    New Member
                    • Aug 2011
                    • 63

                    #10
                    Thank you Rabbit and NeoPa again! It works! :)

                    Comment

                    • michelle copper
                      New Member
                      • Aug 2011
                      • 63

                      #11
                      and Thanks ADezii!! You three have helped me a lot!! :DD

                      Comment

                      Working...