• I don't think that function should worry about time part of date of birth. In SQL2008 you better just to use DATE type so the time wouldn't be present. Pre-2008 I would enforce that DOB DATETIME column would only contain date part, so I wouldn't worry about time in queries which uses it.

    Actually, I'm not even sure that UDF is really required here... I would probaley just use in-line cross apply, something like that:

    -- here is a sample table with DOB as DATE only

    DECLARE @MySample TABLE (DOB DATE)

    INSERT @MySample

    VALUES ('5 Nov 2012'),('3 Nov 2014'),('5 Nov 2011'),('6 Nov 2011'),('4 Apr 1973')

    -- usually it would be single day and most likely it will be today...

    DECLARE @datetoday DATE = GETDATE()

    SELECT M.DOB

    ,AC.AGE

    FROM @MySample M

    CROSS APPLY (SELECT DATEDIFF(yy, M.DOB, @datetoday) -

    CASE WHEN DOB > @datetoday THEN NULL

    WHEN DATEADD(yy, DATEDIFF(yy, DOB, @datetoday), DOB) > @datetoday

    THEN 1 ELSE 0

    END AGE

    ) AC

    Actually, if you want to calculate the AGE on the range of dates, you can use the following:

    -- here is a sample table with DOB as DATE only

    DECLARE @MySample TABLE (DOB DATE)

    INSERT @MySample

    VALUES ('5 Nov 2012'),('3 Nov 2014'),('5 Nov 2011'),('6 Nov 2011'),('4 Apr 1973')

    -- Range of dates:

    DECLARE @Dates TABLE (OnDay DATE)

    INSERT @Dates

    VALUES ('5 Nov 2012'),('10 Nov 2020'),('5 Nov 1970'),('5 Nov 2011'),('4 Apr 2073')

    SELECT M.DOB

    ,D.OnDay

    ,AC.AGE

    FROM @MySample M

    CROSS JOIN @Dates D

    CROSS APPLY (SELECT DATEDIFF(yy, M.DOB, D.OnDay) -

    CASE WHEN DOB > D.OnDay THEN NULL

    WHEN DATEADD(yy, DATEDIFF(yy, DOB, D.OnDay), DOB) > D.OnDay

    THEN 1 ELSE 0

    END AGE

    ) AC

    ORDER BY M.DOB, D.OnDay

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]