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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. calculate age in stored procedure

calculate age in stored procedure

Scheduled Pinned Locked Moved Database
questioncssdatabase
4 Posts 3 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

    I want to know how can I calculate the person's age from a person_birthday datetime field to show the year and month(if any) so if the age is exactly 25 then it should be 25y or if it's less than a year it will be 4m otherwise 25y 2m?

    M P 3 Replies Last reply
    0
    • J Jassim Rahma

      I want to know how can I calculate the person's age from a person_birthday datetime field to show the year and month(if any) so if the age is exactly 25 then it should be 25y or if it's less than a year it will be 4m otherwise 25y 2m?

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

      Use DateDiff(yyyy,birhtday,getdate()) to get the difference, the rest is just an excercise in maths and logic. DATEDIFF is the tool you need.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • J Jassim Rahma

        I want to know how can I calculate the person's age from a person_birthday datetime field to show the year and month(if any) so if the age is exactly 25 then it should be 25y or if it's less than a year it will be 4m otherwise 25y 2m?

        P Offline
        P Offline
        Pranay Rana
        wrote on last edited by
        #3

        try this select cast(DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 as varchar(10))+ ' Y ' + cast (((DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate())) - (DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12) ) as varchar(10)) + 'M' and for your scenario you also have to use case when

        1 Reply Last reply
        0
        • J Jassim Rahma

          I want to know how can I calculate the person's age from a person_birthday datetime field to show the year and month(if any) so if the age is exactly 25 then it should be 25y or if it's less than a year it will be 4m otherwise 25y 2m?

          P Offline
          P Offline
          Pranay Rana
          wrote on last edited by
          #4

          FINAL SOLUTION select case WHEN (DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 ) = 300 THEN (cast(DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 as varchar(10))+ ' Y ') WHEN (DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 ) > 300 THEN (cast(DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 as varchar(10))+ ' Y ' + cast (((DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate())) - ((DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12))*12 ) as varchar(10)) + 'M') WHEN (DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 ) < 300 THEN (cast(DATEDIFF ( month , '1999-10-26 12:30:02.960' , getdate()) /12 as varchar(10))+ ' M ') 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