UFN_AGEFROMFUZZYDATE

Returns a constituent's age at a given time, given his birthdate as a fuzzydate.

Return

Return Type
int

Parameters

Parameter Parameter Type Mode Description
@FuzzyDate char(8) IN
@dteAgeAsOf datetime IN

Definition

Copy


CREATE function dbo.UFN_AGEFROMFUZZYDATE(@FuzzyDate char(8), @dteAgeAsOf datetime)
returns int
with execute as caller
as
begin
    declare @lAge int
    declare @dt datetime
    declare @fz varchar(8)
    declare @year int

    if @FuzzyDate = '00000000' return 0

    if (@FuzzyDate is null) or (len(@FuzzyDate) < 4)
        set @lAge = 0
    else
        begin
            set @fz = @FuzzyDate + '00000000'
            set @year = cast(left(@fz,4) as int)
            if @year < 1753  return 0

            --RDB - fixed bug that happened when no month or day was given for birthdate

            --set @dt = dbo.UFN_YMD(@year, substring(@fz,5,2), substring(@fz,7,2))

            set @dt = dbo.UFN_YMD(@year, coalesce(nullif(substring(@fz,5,2), '00'), '01'), coalesce(nullif(substring(@fz,7,2), '00'), '01'))

            set @lAge = DateDiff(m, @dt, @dteAgeAsOf) / 12

            if month(@dt) = month(@dteAgeAsOf)
                begin
                    if day(@dt) > day(@dteAgeAsOf
                        set @lAge = @lAge - 1
                end
          end

    return(@lAge)
end