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. How to select the records who are completed exactly years comparing with joining date and todays date

How to select the records who are completed exactly years comparing with joining date and todays date

Scheduled Pinned Locked Moved Database
databasecssmysqltutorial
6 Posts 5 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.
  • V Offline
    V Offline
    ven753
    wrote on last edited by
    #1

    Hi, I have mysql database and employee details table is there with the column joining date. i need to write a query in mysql in such a way that i should compare joining date with today date since i need to display employee details who are going to complete exactly 1 yr, 2 yr , 3yr ... and so on. For ex: Joining_Date Current_Date Years_Completed 2010-05-26 2014-05-26 4 years 2000-05-26 2014-05-26 14 years 2010-04-26 2014-05-26 4 years this row should not come since one month is less only for todays date they should exactly complete years How to achieve this. If anybody knows, please reply me. Thanks in advance.

    J M S N 4 Replies Last reply
    0
    • V ven753

      Hi, I have mysql database and employee details table is there with the column joining date. i need to write a query in mysql in such a way that i should compare joining date with today date since i need to display employee details who are going to complete exactly 1 yr, 2 yr , 3yr ... and so on. For ex: Joining_Date Current_Date Years_Completed 2010-05-26 2014-05-26 4 years 2000-05-26 2014-05-26 14 years 2010-04-26 2014-05-26 4 years this row should not come since one month is less only for todays date they should exactly complete years How to achieve this. If anybody knows, please reply me. Thanks in advance.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      What happens with the following joining dates if todays date is 2014-05-26 1. 2012-05-25 2. 2012-05-26 3. 2012-05-27 4. 2013-05-27

      V 1 Reply Last reply
      0
      • V ven753

        Hi, I have mysql database and employee details table is there with the column joining date. i need to write a query in mysql in such a way that i should compare joining date with today date since i need to display employee details who are going to complete exactly 1 yr, 2 yr , 3yr ... and so on. For ex: Joining_Date Current_Date Years_Completed 2010-05-26 2014-05-26 4 years 2000-05-26 2014-05-26 14 years 2010-04-26 2014-05-26 4 years this row should not come since one month is less only for todays date they should exactly complete years How to achieve this. If anybody knows, please reply me. Thanks in advance.

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Don't know MySQL but you should be able to break the datetime object into day/month/year/time components. Select from the table where the day = day and the month = month, ignoring the year and time components.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • J jschell

          What happens with the following joining dates if todays date is 2014-05-26 1. 2012-05-25 2. 2012-05-26 3. 2012-05-27 4. 2013-05-27

          V Offline
          V Offline
          ven753
          wrote on last edited by
          #4

          if todays date is 2014-05-26 then 1. 2012-05-25 if u consider 2 years means 730 (365 X 2) days will come but in this 731 that 2 years 1 day so we should display this, it should complete exactly year 2. 2012-05-26 i think this is coming 730 days means 2 years exactly we can display 3. 2012-05-27 in this 1 day required to complete exactly 2 years now it is 729 days 4. 2013-05-27 in this 364 is coming it should be 365 to complete a year so we should display this record How to achieve this. Please reply me.

          1 Reply Last reply
          0
          • V ven753

            Hi, I have mysql database and employee details table is there with the column joining date. i need to write a query in mysql in such a way that i should compare joining date with today date since i need to display employee details who are going to complete exactly 1 yr, 2 yr , 3yr ... and so on. For ex: Joining_Date Current_Date Years_Completed 2010-05-26 2014-05-26 4 years 2000-05-26 2014-05-26 14 years 2010-04-26 2014-05-26 4 years this row should not come since one month is less only for todays date they should exactly complete years How to achieve this. If anybody knows, please reply me. Thanks in advance.

            S Offline
            S Offline
            Sunasara Imdadhusen
            wrote on last edited by
            #5

            You have to use date formatting in year

            1 Reply Last reply
            0
            • V ven753

              Hi, I have mysql database and employee details table is there with the column joining date. i need to write a query in mysql in such a way that i should compare joining date with today date since i need to display employee details who are going to complete exactly 1 yr, 2 yr , 3yr ... and so on. For ex: Joining_Date Current_Date Years_Completed 2010-05-26 2014-05-26 4 years 2000-05-26 2014-05-26 14 years 2010-04-26 2014-05-26 4 years this row should not come since one month is less only for todays date they should exactly complete years How to achieve this. If anybody knows, please reply me. Thanks in advance.

              N Offline
              N Offline
              NitinDhapte
              wrote on last edited by
              #6

              Hi, Try below :

              DECLARE @JoiningDate DATE
              SET @JoiningDate = '10/10/2013'

              IF (YEAR(GETDATE()) > YEAR(@JoiningDate))
              BEGIN
              IF (DAY(@JoiningDate) = DAY(GETDATE()) AND DAY(@JoiningDate) = DAY(GETDATE()))
              BEGIN
              /*
              This will exceute on every year of joining date.
              Below is the print statement to check the testing result.
              */
              PRINT CONVERT(NVARCHAR,(YEAR(GETDATE()) - YEAR(@JoiningDate))) + ' Year(s)'
              END
              END

              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