Skip to main content

Posts

Showing posts from August, 2008

Simple Age Calculation

I was helping someone with some SQL stuff and needed to give them a function to calculate someone's age.  Rooted around a bunch of places and finally just ended up re-writing it. So this is mostly to have it on hand. This function is needed because DateDiff doesn't check if they've had their birthday this year.CREATEFUNCTION CalculateAge ( @Start smalldatetime , @End smalldatetime ) RETURNSintASBEGIN-- Declare the return variable hereDECLARE @Age intSET @Age = DateDiff(year, @Start, @End) IF DateAdd(year, @Age, @Start) > @EndSET @Age = @Age - 1 RETURN @Age END GO VB VersionPublicFunction Age(ByVal value AsDate, ByVal d AsDate) AsInteger Age = DateDiff(DateInterval.Year, value, d) If value.AddYears(Age) > d Then Age = Age - 1 Return Age EndFunction Technorati Tags: ,,,,