MSSQL: Calculating Age (in years) :: right -v- wrong

Published on
6,145 Points
1,345 Views
3 Endorsements
When calculating a person's age; year, month and day-of-month all need to be compared to the current year, month and day-of-month. Attempting to do this by a formula can only be an approximation and many common methods that are proposed for this calulation are incorrect, e.g.
, DATEDIFF(DAY, BirthDate, GETDATE()) / 365
, DATEDIFF(YEAR, BirthDate, GETDATE())

These simplistic approaches ignore the true impact of day/month when comparing to another date, and will frequently overstate age by 1 year     (the datediff(day method is 'better' but sill not great).

A reliable method is to do all 3 needed comparisons (year, month and day-of-month)  with the assisnace of a case expression like so:

, YEAR(getdate()) - YEAR(BirthDate)                                   --<< year comparison
- ( CASE
WHEN MONTH(BirthDate) > MONTH(getdate())   --<< month comparison
OR ( MONTH(BirthDate) = MONTH(getdate())
AND DAY(BirthDate) > DAY(getdate())               --<< day-of-month comparison
)
THEN 1
ELSE 0
END )                                AS Reliable_Age

This can be compared to other calculation methods, here I have used a common table expression (CTE) to setup a range of birthdates

``````    /* demonstrating calculation of age against 2 unreliable methods */
;WITH
CTE AS (
SELECT
CAST('19100101' AS datetime) AS BirthDate
UNION ALL
SELECT DATEADD(DAY, 1, BirthDate)
FROM CTE
WHERE BirthDate < DATEADD(DAY, -1, GETDATE())
)
, CTE2 AS (
SELECT
BirthDate
, YEAR(getdate()) - YEAR(BirthDate)
- ( CASE
WHEN MONTH(BirthDate) > MONTH(getdate())
OR ( MONTH(BirthDate) = MONTH(getdate())
AND DAY(BirthDate) > DAY(getdate())
)
THEN 1
ELSE 0
END )                                AS Reliable_Age
, DATEDIFF(DAY, BirthDate, GETDATE()) / 365 AS Unreliable_Age_1
, DATEDIFF(YEAR, BirthDate, GETDATE())      AS Unreliable_Age_2
FROM CTE
)
SELECT
convert(varchar(10), getdate(), 121)      AS Today
, count(BirthDate)                          AS count_BirthDate
, convert(varchar(10), min(BirthDate), 121) AS min_BirthDate
, convert(varchar(10), max(BirthDate), 121) AS max_BirthDate
, Reliable_Age
, Unreliable_Age_1
, Unreliable_Age_2
FROM CTE2
WHERE Reliable_Age <> Unreliable_Age_1
OR Reliable_Age <> Unreliable_Age_2
GROUP BY
Reliable_Age
, Unreliable_Age_1
, Unreliable_Age_2
ORDER BY
min_BirthDate

OPTION (MAXRECURSION 0)
``````
and the result table of that is:

notice how frequetly the last 2 columns disagree with the reliable calculation
``````    |      TODAY | COUNT_BIRTHDATE | MIN_BIRTHDATE | MAX_BIRTHDATE | RELIABLE_AGE | UNRELIABLE_AGE_1 | UNRELIABLE_AGE_2 |
|------------|-----------------|---------------|---------------|--------------|------------------|------------------|
| 2013-10-02 |              26 |    1910-10-03 |    1910-10-28 |          102 |              103 |              103 |
| 2013-10-02 |              64 |    1910-10-29 |    1910-12-31 |          102 |              102 |              103 |
| 2013-10-02 |              26 |    1911-10-03 |    1911-10-28 |          101 |              102 |              102 |
| 2013-10-02 |              64 |    1911-10-29 |    1911-12-31 |          101 |              101 |              102 |
| 2013-10-02 |              25 |    1912-10-03 |    1912-10-27 |          100 |              101 |              101 |
| 2013-10-02 |              65 |    1912-10-28 |    1912-12-31 |          100 |              100 |              101 |
| 2013-10-02 |              25 |    1913-10-03 |    1913-10-27 |           99 |              100 |              100 |
| 2013-10-02 |              65 |    1913-10-28 |    1913-12-31 |           99 |               99 |              100 |
| 2013-10-02 |              25 |    1914-10-03 |    1914-10-27 |           98 |               99 |               99 |
| 2013-10-02 |              65 |    1914-10-28 |    1914-12-31 |           98 |               98 |               99 |
| 2013-10-02 |              25 |    1915-10-03 |    1915-10-27 |           97 |               98 |               98 |
| 2013-10-02 |              65 |    1915-10-28 |    1915-12-31 |           97 |               97 |               98 |
| 2013-10-02 |              24 |    1916-10-03 |    1916-10-26 |           96 |               97 |               97 |
| 2013-10-02 |              66 |    1916-10-27 |    1916-12-31 |           96 |               96 |               97 |
| 2013-10-02 |              24 |    1917-10-03 |    1917-10-26 |           95 |               96 |               96 |
| 2013-10-02 |              66 |    1917-10-27 |    1917-12-31 |           95 |               95 |               96 |
| 2013-10-02 |              24 |    1918-10-03 |    1918-10-26 |           94 |               95 |               95 |
| 2013-10-02 |              66 |    1918-10-27 |    1918-12-31 |           94 |               94 |               95 |
| 2013-10-02 |              24 |    1919-10-03 |    1919-10-26 |           93 |               94 |               94 |
| 2013-10-02 |              66 |    1919-10-27 |    1919-12-31 |           93 |               93 |               94 |
| 2013-10-02 |              23 |    1920-10-03 |    1920-10-25 |           92 |               93 |               93 |
| 2013-10-02 |              67 |    1920-10-26 |    1920-12-31 |           92 |               92 |               93 |
| 2013-10-02 |              23 |    1921-10-03 |    1921-10-25 |           91 |               92 |               92 |
| 2013-10-02 |              67 |    1921-10-26 |    1921-12-31 |           91 |               91 |               92 |
| 2013-10-02 |              23 |    1922-10-03 |    1922-10-25 |           90 |               91 |               91 |
| 2013-10-02 |              67 |    1922-10-26 |    1922-12-31 |           90 |               90 |               91 |
| 2013-10-02 |              23 |    1923-10-03 |    1923-10-25 |           89 |               90 |               90 |
| 2013-10-02 |              67 |    1923-10-26 |    1923-12-31 |           89 |               89 |               90 |
| 2013-10-02 |              22 |    1924-10-03 |    1924-10-24 |           88 |               89 |               89 |
| 2013-10-02 |              68 |    1924-10-25 |    1924-12-31 |           88 |               88 |               89 |
| 2013-10-02 |              22 |    1925-10-03 |    1925-10-24 |           87 |               88 |               88 |
| 2013-10-02 |              68 |    1925-10-25 |    1925-12-31 |           87 |               87 |               88 |
| 2013-10-02 |              22 |    1926-10-03 |    1926-10-24 |           86 |               87 |               87 |
| 2013-10-02 |              68 |    1926-10-25 |    1926-12-31 |           86 |               86 |               87 |
| 2013-10-02 |              22 |    1927-10-03 |    1927-10-24 |           85 |               86 |               86 |
| 2013-10-02 |              68 |    1927-10-25 |    1927-12-31 |           85 |               85 |               86 |
| 2013-10-02 |              21 |    1928-10-03 |    1928-10-23 |           84 |               85 |               85 |
| 2013-10-02 |              69 |    1928-10-24 |    1928-12-31 |           84 |               84 |               85 |
| 2013-10-02 |              21 |    1929-10-03 |    1929-10-23 |           83 |               84 |               84 |
| 2013-10-02 |              69 |    1929-10-24 |    1929-12-31 |           83 |               83 |               84 |
| 2013-10-02 |              21 |    1930-10-03 |    1930-10-23 |           82 |               83 |               83 |
| 2013-10-02 |              69 |    1930-10-24 |    1930-12-31 |           82 |               82 |               83 |
| 2013-10-02 |              21 |    1931-10-03 |    1931-10-23 |           81 |               82 |               82 |
| 2013-10-02 |              69 |    1931-10-24 |    1931-12-31 |           81 |               81 |               82 |
| 2013-10-02 |              20 |    1932-10-03 |    1932-10-22 |           80 |               81 |               81 |
| 2013-10-02 |              70 |    1932-10-23 |    1932-12-31 |           80 |               80 |               81 |
| 2013-10-02 |              20 |    1933-10-03 |    1933-10-22 |           79 |               80 |               80 |
| 2013-10-02 |              70 |    1933-10-23 |    1933-12-31 |           79 |               79 |               80 |
| 2013-10-02 |              20 |    1934-10-03 |    1934-10-22 |           78 |               79 |               79 |
| 2013-10-02 |              70 |    1934-10-23 |    1934-12-31 |           78 |               78 |               79 |
| 2013-10-02 |              20 |    1935-10-03 |    1935-10-22 |           77 |               78 |               78 |
| 2013-10-02 |              70 |    1935-10-23 |    1935-12-31 |           77 |               77 |               78 |
| 2013-10-02 |              19 |    1936-10-03 |    1936-10-21 |           76 |               77 |               77 |
| 2013-10-02 |              71 |    1936-10-22 |    1936-12-31 |           76 |               76 |               77 |
| 2013-10-02 |              19 |    1937-10-03 |    1937-10-21 |           75 |               76 |               76 |
| 2013-10-02 |              71 |    1937-10-22 |    1937-12-31 |           75 |               75 |               76 |
| 2013-10-02 |              19 |    1938-10-03 |    1938-10-21 |           74 |               75 |               75 |
| 2013-10-02 |              71 |    1938-10-22 |    1938-12-31 |           74 |               74 |               75 |
| 2013-10-02 |              19 |    1939-10-03 |    1939-10-21 |           73 |               74 |               74 |
| 2013-10-02 |              71 |    1939-10-22 |    1939-12-31 |           73 |               73 |               74 |
| 2013-10-02 |              18 |    1940-10-03 |    1940-10-20 |           72 |               73 |               73 |
| 2013-10-02 |              72 |    1940-10-21 |    1940-12-31 |           72 |               72 |               73 |
| 2013-10-02 |              18 |    1941-10-03 |    1941-10-20 |           71 |               72 |               72 |
| 2013-10-02 |              72 |    1941-10-21 |    1941-12-31 |           71 |               71 |               72 |
| 2013-10-02 |              18 |    1942-10-03 |    1942-10-20 |           70 |               71 |               71 |
| 2013-10-02 |              72 |    1942-10-21 |    1942-12-31 |           70 |               70 |               71 |
| 2013-10-02 |              18 |    1943-10-03 |    1943-10-20 |           69 |               70 |               70 |
| 2013-10-02 |              72 |    1943-10-21 |    1943-12-31 |           69 |               69 |               70 |
| 2013-10-02 |              17 |    1944-10-03 |    1944-10-19 |           68 |               69 |               69 |
| 2013-10-02 |              73 |    1944-10-20 |    1944-12-31 |           68 |               68 |               69 |
| 2013-10-02 |              17 |    1945-10-03 |    1945-10-19 |           67 |               68 |               68 |
| 2013-10-02 |              73 |    1945-10-20 |    1945-12-31 |           67 |               67 |               68 |
| 2013-10-02 |              17 |    1946-10-03 |    1946-10-19 |           66 |               67 |               67 |
| 2013-10-02 |              73 |    1946-10-20 |    1946-12-31 |           66 |               66 |               67 |
| 2013-10-02 |              17 |    1947-10-03 |    1947-10-19 |           65 |               66 |               66 |
| 2013-10-02 |              73 |    1947-10-20 |    1947-12-31 |           65 |               65 |               66 |
| 2013-10-02 |              16 |    1948-10-03 |    1948-10-18 |           64 |               65 |               65 |
| 2013-10-02 |              74 |    1948-10-19 |    1948-12-31 |           64 |               64 |               65 |
| 2013-10-02 |              16 |    1949-10-03 |    1949-10-18 |           63 |               64 |               64 |
| 2013-10-02 |              74 |    1949-10-19 |    1949-12-31 |           63 |               63 |               64 |
| 2013-10-02 |              16 |    1950-10-03 |    1950-10-18 |           62 |               63 |               63 |
| 2013-10-02 |              74 |    1950-10-19 |    1950-12-31 |           62 |               62 |               63 |
| 2013-10-02 |              16 |    1951-10-03 |    1951-10-18 |           61 |               62 |               62 |
| 2013-10-02 |              74 |    1951-10-19 |    1951-12-31 |           61 |               61 |               62 |
| 2013-10-02 |              15 |    1952-10-03 |    1952-10-17 |           60 |               61 |               61 |
| 2013-10-02 |              75 |    1952-10-18 |    1952-12-31 |           60 |               60 |               61 |
| 2013-10-02 |              15 |    1953-10-03 |    1953-10-17 |           59 |               60 |               60 |
| 2013-10-02 |              75 |    1953-10-18 |    1953-12-31 |           59 |               59 |               60 |
| 2013-10-02 |              15 |    1954-10-03 |    1954-10-17 |           58 |               59 |               59 |
| 2013-10-02 |              75 |    1954-10-18 |    1954-12-31 |           58 |               58 |               59 |
| 2013-10-02 |              15 |    1955-10-03 |    1955-10-17 |           57 |               58 |               58 |
| 2013-10-02 |              75 |    1955-10-18 |    1955-12-31 |           57 |               57 |               58 |
| 2013-10-02 |              14 |    1956-10-03 |    1956-10-16 |           56 |               57 |               57 |
| 2013-10-02 |              76 |    1956-10-17 |    1956-12-31 |           56 |               56 |               57 |
| 2013-10-02 |              14 |    1957-10-03 |    1957-10-16 |           55 |               56 |               56 |
| 2013-10-02 |              76 |    1957-10-17 |    1957-12-31 |           55 |               55 |               56 |
| 2013-10-02 |              14 |    1958-10-03 |    1958-10-16 |           54 |               55 |               55 |
| 2013-10-02 |              76 |    1958-10-17 |    1958-12-31 |           54 |               54 |               55 |
| 2013-10-02 |              14 |    1959-10-03 |    1959-10-16 |           53 |               54 |               54 |
| 2013-10-02 |              76 |    1959-10-17 |    1959-12-31 |           53 |               53 |               54 |
| 2013-10-02 |              13 |    1960-10-03 |    1960-10-15 |           52 |               53 |               53 |
| 2013-10-02 |              77 |    1960-10-16 |    1960-12-31 |           52 |               52 |               53 |
| 2013-10-02 |              13 |    1961-10-03 |    1961-10-15 |           51 |               52 |               52 |
| 2013-10-02 |              77 |    1961-10-16 |    1961-12-31 |           51 |               51 |               52 |
| 2013-10-02 |              13 |    1962-10-03 |    1962-10-15 |           50 |               51 |               51 |
| 2013-10-02 |              77 |    1962-10-16 |    1962-12-31 |           50 |               50 |               51 |
| 2013-10-02 |              13 |    1963-10-03 |    1963-10-15 |           49 |               50 |               50 |
| 2013-10-02 |              77 |    1963-10-16 |    1963-12-31 |           49 |               49 |               50 |
| 2013-10-02 |              12 |    1964-10-03 |    1964-10-14 |           48 |               49 |               49 |
| 2013-10-02 |              78 |    1964-10-15 |    1964-12-31 |           48 |               48 |               49 |
| 2013-10-02 |              12 |    1965-10-03 |    1965-10-14 |           47 |               48 |               48 |
| 2013-10-02 |              78 |    1965-10-15 |    1965-12-31 |           47 |               47 |               48 |
| 2013-10-02 |              12 |    1966-10-03 |    1966-10-14 |           46 |               47 |               47 |
| 2013-10-02 |              78 |    1966-10-15 |    1966-12-31 |           46 |               46 |               47 |
| 2013-10-02 |              12 |    1967-10-03 |    1967-10-14 |           45 |               46 |               46 |
| 2013-10-02 |              78 |    1967-10-15 |    1967-12-31 |           45 |               45 |               46 |
| 2013-10-02 |              11 |    1968-10-03 |    1968-10-13 |           44 |               45 |               45 |
| 2013-10-02 |              79 |    1968-10-14 |    1968-12-31 |           44 |               44 |               45 |
| 2013-10-02 |              11 |    1969-10-03 |    1969-10-13 |           43 |               44 |               44 |
| 2013-10-02 |              79 |    1969-10-14 |    1969-12-31 |           43 |               43 |               44 |
| 2013-10-02 |              11 |    1970-10-03 |    1970-10-13 |           42 |               43 |               43 |
| 2013-10-02 |              79 |    1970-10-14 |    1970-12-31 |           42 |               42 |               43 |
| 2013-10-02 |              11 |    1971-10-03 |    1971-10-13 |           41 |               42 |               42 |
| 2013-10-02 |              79 |    1971-10-14 |    1971-12-31 |           41 |               41 |               42 |
| 2013-10-02 |              10 |    1972-10-03 |    1972-10-12 |           40 |               41 |               41 |
| 2013-10-02 |              80 |    1972-10-13 |    1972-12-31 |           40 |               40 |               41 |
| 2013-10-02 |              10 |    1973-10-03 |    1973-10-12 |           39 |               40 |               40 |
| 2013-10-02 |              80 |    1973-10-13 |    1973-12-31 |           39 |               39 |               40 |
| 2013-10-02 |              10 |    1974-10-03 |    1974-10-12 |           38 |               39 |               39 |
| 2013-10-02 |              80 |    1974-10-13 |    1974-12-31 |           38 |               38 |               39 |
| 2013-10-02 |              10 |    1975-10-03 |    1975-10-12 |           37 |               38 |               38 |
| 2013-10-02 |              80 |    1975-10-13 |    1975-12-31 |           37 |               37 |               38 |
| 2013-10-02 |               9 |    1976-10-03 |    1976-10-11 |           36 |               37 |               37 |
| 2013-10-02 |              81 |    1976-10-12 |    1976-12-31 |           36 |               36 |               37 |
| 2013-10-02 |               9 |    1977-10-03 |    1977-10-11 |           35 |               36 |               36 |
| 2013-10-02 |              81 |    1977-10-12 |    1977-12-31 |           35 |               35 |               36 |
| 2013-10-02 |               9 |    1978-10-03 |    1978-10-11 |           34 |               35 |               35 |
| 2013-10-02 |              81 |    1978-10-12 |    1978-12-31 |           34 |               34 |               35 |
| 2013-10-02 |               9 |    1979-10-03 |    1979-10-11 |           33 |               34 |               34 |
| 2013-10-02 |              81 |    1979-10-12 |    1979-12-31 |           33 |               33 |               34 |
| 2013-10-02 |               8 |    1980-10-03 |    1980-10-10 |           32 |               33 |               33 |
| 2013-10-02 |              82 |    1980-10-11 |    1980-12-31 |           32 |               32 |               33 |
| 2013-10-02 |               8 |    1981-10-03 |    1981-10-10 |           31 |               32 |               32 |
| 2013-10-02 |              82 |    1981-10-11 |    1981-12-31 |           31 |               31 |               32 |
| 2013-10-02 |               8 |    1982-10-03 |    1982-10-10 |           30 |               31 |               31 |
| 2013-10-02 |              82 |    1982-10-11 |    1982-12-31 |           30 |               30 |               31 |
| 2013-10-02 |               8 |    1983-10-03 |    1983-10-10 |           29 |               30 |               30 |
| 2013-10-02 |              82 |    1983-10-11 |    1983-12-31 |           29 |               29 |               30 |
| 2013-10-02 |               7 |    1984-10-03 |    1984-10-09 |           28 |               29 |               29 |
| 2013-10-02 |              83 |    1984-10-10 |    1984-12-31 |           28 |               28 |               29 |
| 2013-10-02 |               7 |    1985-10-03 |    1985-10-09 |           27 |               28 |               28 |
| 2013-10-02 |              83 |    1985-10-10 |    1985-12-31 |           27 |               27 |               28 |
| 2013-10-02 |               7 |    1986-10-03 |    1986-10-09 |           26 |               27 |               27 |
| 2013-10-02 |              83 |    1986-10-10 |    1986-12-31 |           26 |               26 |               27 |
| 2013-10-02 |               7 |    1987-10-03 |    1987-10-09 |           25 |               26 |               26 |
| 2013-10-02 |              83 |    1987-10-10 |    1987-12-31 |           25 |               25 |               26 |
| 2013-10-02 |               6 |    1988-10-03 |    1988-10-08 |           24 |               25 |               25 |
| 2013-10-02 |              84 |    1988-10-09 |    1988-12-31 |           24 |               24 |               25 |
| 2013-10-02 |               6 |    1989-10-03 |    1989-10-08 |           23 |               24 |               24 |
| 2013-10-02 |              84 |    1989-10-09 |    1989-12-31 |           23 |               23 |               24 |
| 2013-10-02 |               6 |    1990-10-03 |    1990-10-08 |           22 |               23 |               23 |
| 2013-10-02 |              84 |    1990-10-09 |    1990-12-31 |           22 |               22 |               23 |
| 2013-10-02 |               6 |    1991-10-03 |    1991-10-08 |           21 |               22 |               22 |
| 2013-10-02 |              84 |    1991-10-09 |    1991-12-31 |           21 |               21 |               22 |
| 2013-10-02 |               5 |    1992-10-03 |    1992-10-07 |           20 |               21 |               21 |
| 2013-10-02 |              85 |    1992-10-08 |    1992-12-31 |           20 |               20 |               21 |
| 2013-10-02 |               5 |    1993-10-03 |    1993-10-07 |           19 |               20 |               20 |
| 2013-10-02 |              85 |    1993-10-08 |    1993-12-31 |           19 |               19 |               20 |
| 2013-10-02 |               5 |    1994-10-03 |    1994-10-07 |           18 |               19 |               19 |
| 2013-10-02 |              85 |    1994-10-08 |    1994-12-31 |           18 |               18 |               19 |
| 2013-10-02 |               5 |    1995-10-03 |    1995-10-07 |           17 |               18 |               18 |
| 2013-10-02 |              85 |    1995-10-08 |    1995-12-31 |           17 |               17 |               18 |
| 2013-10-02 |               4 |    1996-10-03 |    1996-10-06 |           16 |               17 |               17 |
| 2013-10-02 |              86 |    1996-10-07 |    1996-12-31 |           16 |               16 |               17 |
| 2013-10-02 |               4 |    1997-10-03 |    1997-10-06 |           15 |               16 |               16 |
| 2013-10-02 |              86 |    1997-10-07 |    1997-12-31 |           15 |               15 |               16 |
| 2013-10-02 |               4 |    1998-10-03 |    1998-10-06 |           14 |               15 |               15 |
| 2013-10-02 |              86 |    1998-10-07 |    1998-12-31 |           14 |               14 |               15 |
| 2013-10-02 |               4 |    1999-10-03 |    1999-10-06 |           13 |               14 |               14 |
| 2013-10-02 |              86 |    1999-10-07 |    1999-12-31 |           13 |               13 |               14 |
| 2013-10-02 |               3 |    2000-10-03 |    2000-10-05 |           12 |               13 |               13 |
| 2013-10-02 |              87 |    2000-10-06 |    2000-12-31 |           12 |               12 |               13 |
| 2013-10-02 |               3 |    2001-10-03 |    2001-10-05 |           11 |               12 |               12 |
| 2013-10-02 |              87 |    2001-10-06 |    2001-12-31 |           11 |               11 |               12 |
| 2013-10-02 |               3 |    2002-10-03 |    2002-10-05 |           10 |               11 |               11 |
| 2013-10-02 |              87 |    2002-10-06 |    2002-12-31 |           10 |               10 |               11 |
| 2013-10-02 |               3 |    2003-10-03 |    2003-10-05 |            9 |               10 |               10 |
| 2013-10-02 |              87 |    2003-10-06 |    2003-12-31 |            9 |                9 |               10 |
| 2013-10-02 |               2 |    2004-10-03 |    2004-10-04 |            8 |                9 |                9 |
| 2013-10-02 |              88 |    2004-10-05 |    2004-12-31 |            8 |                8 |                9 |
| 2013-10-02 |               2 |    2005-10-03 |    2005-10-04 |            7 |                8 |                8 |
| 2013-10-02 |              88 |    2005-10-05 |    2005-12-31 |            7 |                7 |                8 |
| 2013-10-02 |               2 |    2006-10-03 |    2006-10-04 |            6 |                7 |                7 |
| 2013-10-02 |              88 |    2006-10-05 |    2006-12-31 |            6 |                6 |                7 |
| 2013-10-02 |               2 |    2007-10-03 |    2007-10-04 |            5 |                6 |                6 |
| 2013-10-02 |              88 |    2007-10-05 |    2007-12-31 |            5 |                5 |                6 |
| 2013-10-02 |               1 |    2008-10-03 |    2008-10-03 |            4 |                5 |                5 |
| 2013-10-02 |              89 |    2008-10-04 |    2008-12-31 |            4 |                4 |                5 |
| 2013-10-02 |               1 |    2009-10-03 |    2009-10-03 |            3 |                4 |                4 |
| 2013-10-02 |              89 |    2009-10-04 |    2009-12-31 |            3 |                3 |                4 |
| 2013-10-02 |               1 |    2010-10-03 |    2010-10-03 |            2 |                3 |                3 |
| 2013-10-02 |              89 |    2010-10-04 |    2010-12-31 |            2 |                2 |                3 |
| 2013-10-02 |               1 |    2011-10-03 |    2011-10-03 |            1 |                2 |                2 |
| 2013-10-02 |              89 |    2011-10-04 |    2011-12-31 |            1 |                1 |                2 |
| 2013-10-02 |              90 |    2012-10-03 |    2012-12-31 |            0 |                0 |                1 |
``````

Demonstrated by recursive CTE here:  http://sqlfiddle.com/#!3/a6491/4

3
Author:PortletPaul
• 2

LVL 47

Expert Comment

1. rather than getdate(), a stored proc should accept both birthdate and AgeOnDate parameters.

2. I've run some tests in the Access environment and the performance of the Case statement can be improved if you convert the month and day of both the birthdate and the AgeOnDate with the Month() and Day() functions, multiplying the Month() result by 100 and adding the Day() result.  You can use the comparison as part of your year difference calculation.  I even ditched the DateDiff() function for a subtraction of the two Year() functions.

3. It would be helpful to your reader if you explained how the DateDiff() function works in general.  This will help your reader understand the function's use and applicability in context.
0

LVL 55

Expert Comment

@PortletPaul,
Can you explain what each field means? Because for your example I would say that Unreliable ages are both correct but reliable age it isn't.
0

LVL 50

Author Comment

Oracle syntax
``````select
CustomerID
, DOB
, VisitDate
, EXTRACT(YEAR FROM VisitDate) - EXTRACT(YEAR FROM dob)
- ( CASE
WHEN EXTRACT(MONTH FROM dob) > EXTRACT(MONTH FROM VisitDate)
OR ( EXTRACT(MONTH FROM dob) = EXTRACT(MONTH FROM VisitDate)
AND EXTRACT(DAY FROM dob) > EXTRACT(DAY FROM VisitDate)
)
THEN 1
ELSE 0
END )    AS age_at_visit
from sample
``````
0

LVL 47

Expert Comment

@PortletPaul

This question is in the SQL Server zone.  You just posted "Oracle syntax".  Is this an Oracle question?
0

Join & Write a Comment Already a member? Login.

Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month