blog.easyciel.net author="Patrick Rabian" about="c#, sharepoint, biztalk, team system resources" more="news, samples, tips for .NET world's developers !"

Get age from date of birth in Transact Sql

Monday, 12 December 2005 11:55 by prabian

Calculate the age of a person from his date of birth.

Parameters :

@dateNaissance = Date of birth
@dateActuelle = Current date (from GetDate())

CREATE FUNCTION [dbo].[fnGetAge] (@dateNaissance SMALLDATETIME, @dateActuelle SMALLDATETIME)
RETURNS INT AS
BEGIN
DECLARE @age INT
SET @age = FLOOR(DateDiff(dd,@dateNaissance,@dateActuelle)/365.25)

IF (Day(@dateNaissance) = Day(@dateActuelle)) AND (Month(@dateNaissance) = Month(@dateActuelle))
   BEGIN
      SET @age = @age + 1
   END

RETURN @age
END

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   DAL | Sql
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 21. 2008 01:38