Hello Friends, In this tutorial I will show you a simple User Defined Function to calculate Age from Date Of Birth in SQL Server.

User Defined Function to Calculate Age from Date Of Birth in SQL Server:

CREATE FUNCTION udfGetAge
(
@date datetime
)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE @tmpdate datetime, @years int, @months int, @days int


SELECT @tmpdate = @date

SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())
RETURN 'Your Age is '+ CAST(@years AS VARCHAR(10))+' Year,'+ CAST(@months AS VARCHAR(10))+' Month,'+ CAST(@days AS VARCHAR(10))+' Days'
END

Test Function:

SELECT [dbo].[udfGetAge]('06/20/1993')

Output:

Calculate Age

View More:

Conclusion:

I hope you would love this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are always appreciated.

Thank You.

25 Comments

  • I happen to be writing to let you be aware of what a nice experience my friend’s princess found using your web page. She learned a lot of things, with the inclusion of how it is like to have a very effective teaching spirit to get the rest without hassle fully grasp selected multifaceted matters. You actually exceeded her expectations. Many thanks for producing the interesting, healthy, edifying and unique tips about this topic to Jane.

  • Por eso, contamos con un servicio de emergencias de fin de semana y además, durante la semana, contratando nuestro servicio VIP, garantizamos la mejor asistencia en menos de 2 horas. Para ofrecer la mejor cobertura, contamos con una extensa red de centros por toda la geografía española. Además, en disponemos de un localizador para encontrar muy fácilmente el Centro de Asistencia Técnica más cercano. Todos nuestros técnicos disponen del carnet oficial ANFEL (Asociación Nacional de Fabricantes de Electrodomésticos), lo que acredita su profesionalidad, seguridad y confianza. Ofrecemos reparaciones y servicio técnico de todas las marcas los 3 5 días del año.

  • What i don’t understood is if truth be told how you are no longer really a lot more neatly-favored than you might be right now. You’re very intelligent. You know thus significantly when it comes to this subject, made me individually consider it from a lot of varied angles. Its like women and men aren’t interested until it is one thing to do with Girl gaga! Your personal stuffs great. All the time take care of it up!

  • Maximize your by how a large amount of gear are employed internationally and will often impart numerous memory making use of that your is also fighting that is a result from our team rrnside the twenty initial centuries. everyday deal livingsocial discount baltimore washington.

  • I抳e recently started a website, the information you offer on this website has helped me tremendously. Thanks for all of your time & work.

  • Howdy, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam responses? If so how do you stop it, any plugin or anything you can suggest? I get so much lately it’s driving me insane so any assistance is very much appreciated.

  • Undeniably believe that which you stated. Your favorite reason appeared to be on the web the simplest thing to be aware of. I say to you, I certainly get irked while people think about worries that they plainly don’t know about. You managed to hit the nail upon the top and defined out the whole thing without having side-effects , people can take a signal. Will probably be back to get more. Thanks|

  • May I simply just say what a relief to discover an individual who truly understands what they’re talking about over the internet. You actually know how to bring a problem to light and make it important. More people really need to check this out and understand this side of the story. I was surprised you aren’t more popular since you most certainly have the gift.
    http://georgesj703vjx2.aboutyoublog.com/profile

  • May I simply just say what a relief to discover someone who actually understands what they’re talking about online. You actually understand how to bring an issue to light and make it important. More and more people really need to check this out and understand this side of the story. It’s surprising you aren’t more popular since you certainly possess the gift.
    http://jonathano925cpd4.csublogs.com/profile

  • Velocidad de corte – Las máquinas CNC pueden crear componentes más rápidamente ya que no requieren tantas configuraciones, cambios de herramientas y accesorios especiales en comparación con las técnicas de mecanizado manual. Normalmente las palabras que más va a usar el control de una máquina CNC son entre 40 y 50, por lo que se trata de un lenguaje relativamente sencillo.

  • Can I just say what a relief to uncover someone that truly understands what they are talking about on the web. You actually understand how to bring an issue to light and make it important. More people need to read this and understand this side of the story. It’s surprising you’re not more popular given that you surely possess the gift.
    http://francisco37m81.tinyblogging.com/Facts-About-Revealed-37290434

  • May I simply just say what a relief to uncover someone who actually understands what they’re discussing on the web. You definitely realize how to bring an issue to light and make it important. More and more people ought to read this and understand this side of the story. I was surprised you are not more popular given that you most certainly have the gift.
    https://hippof697lct1.blogspothub.com/profile

  • Can I just say what a relief to find someone that really knows what they’re discussing on the internet. You actually realize how to bring a problem to light and make it important. More people have to read this and understand this side of your story. I was surprised you’re not more popular because you most certainly possess the gift.
    http://fredericw594gcy4.blogsidea.com/profile