Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. (Expiring Next Month) Problem

(Expiring Next Month) Problem

Scheduled Pinned Locked Moved Database
questioncomhelpannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, I have expiry_date field. I am using nthis code to get expired this month list.

    SELECT contracts.contract_id, contract_types.type_name, contracts.contract_name, contracts.supplier, contracts.contact_name, contracts.contact_number, contracts.expiry_date, contracts.reminder FROM contracts
    JOIN contract_types ON contract_types.type_id = contracts.type
    WHERE MONTH(contracts.expiry_date) = MONTH(NOW()) AND YEAR(contracts.expiry_date) = YEAR(NOW());

    Now, I would like to get list of expiring next month. How can I do this? what if current month is 12? then Next Month will be 1 and Year will also be changed? Kindly advise

    Technology News @ www.JassimRahma.com

    Kornfeld Eliyahu PeterK G 2 Replies Last reply
    0
    • J Jassim Rahma

      Hi, I have expiry_date field. I am using nthis code to get expired this month list.

      SELECT contracts.contract_id, contract_types.type_name, contracts.contract_name, contracts.supplier, contracts.contact_name, contracts.contact_number, contracts.expiry_date, contracts.reminder FROM contracts
      JOIN contract_types ON contract_types.type_id = contracts.type
      WHERE MONTH(contracts.expiry_date) = MONTH(NOW()) AND YEAR(contracts.expiry_date) = YEAR(NOW());

      Now, I would like to get list of expiring next month. How can I do this? what if current month is 12? then Next Month will be 1 and Year will also be changed? Kindly advise

      Technology News @ www.JassimRahma.com

      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      WHERE MONTH(contracts.expiry_date) = DATE_ADD(MONTH(NOW()), INTERVAL 1 MONTH)

      http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html[^]

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      J 1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, I have expiry_date field. I am using nthis code to get expired this month list.

        SELECT contracts.contract_id, contract_types.type_name, contracts.contract_name, contracts.supplier, contracts.contact_name, contracts.contact_number, contracts.expiry_date, contracts.reminder FROM contracts
        JOIN contract_types ON contract_types.type_id = contracts.type
        WHERE MONTH(contracts.expiry_date) = MONTH(NOW()) AND YEAR(contracts.expiry_date) = YEAR(NOW());

        Now, I would like to get list of expiring next month. How can I do this? what if current month is 12? then Next Month will be 1 and Year will also be changed? Kindly advise

        Technology News @ www.JassimRahma.com

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        Have a look at the DATEDIFF function. E.g.

        select datediff(mm,getdate(),getdate()+180)

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        1 Reply Last reply
        0
        • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

          WHERE MONTH(contracts.expiry_date) = DATE_ADD(MONTH(NOW()), INTERVAL 1 MONTH)

          http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html[^]

          I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          this solved my problem.

          WHERE MONTH(contracts.expiry_date) = MONTH(DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH)) AND YEAR(contracts.expiry_date) = YEAR(DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH));

          Thank you so much

          Technology News @ www.JassimRahma.com

          G 1 Reply Last reply
          0
          • J Jassim Rahma

            this solved my problem.

            WHERE MONTH(contracts.expiry_date) = MONTH(DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH)) AND YEAR(contracts.expiry_date) = YEAR(DATE_ADD(DATE(NOW()), INTERVAL 1 MONTH));

            Thank you so much

            Technology News @ www.JassimRahma.com

            G Offline
            G Offline
            GuyThiebaut
            wrote on last edited by
            #5

            This may be easier to read and you won't need to do calculations on years:

            WHERE DATEDIFF(mm,contracts.expiry_date,DATE_ADD(NOW(), INTERVAL 1 MONTH)) = 1

            “That which can be asserted without evidence, can be dismissed without evidence.”

            ― Christopher Hitchens

            B 1 Reply Last reply
            0
            • G GuyThiebaut

              This may be easier to read and you won't need to do calculations on years:

              WHERE DATEDIFF(mm,contracts.expiry_date,DATE_ADD(NOW(), INTERVAL 1 MONTH)) = 1

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              Hm, depends on the details of his requirements. If I see it correctly, your query will select all data within one month from now. His question looked like he needed the data of next calendar month.

              G 1 Reply Last reply
              0
              • B Bernhard Hiller

                Hm, depends on the details of his requirements. If I see it correctly, your query will select all data within one month from now. His question looked like he needed the data of next calendar month.

                G Offline
                G Offline
                GuyThiebaut
                wrote on last edited by
                #7

                Try the query out - you will find that it selects the data from next calendar month ;)

                “That which can be asserted without evidence, can be dismissed without evidence.”

                ― Christopher Hitchens

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups