This excerpt will convert a date of birth into an age. It returns NULL when no DoB is present.
CASE WHEN T.dwoDOB IS NULL THEN NULL ELSE CASE WHEN DATEADD(YEAR, DATEDIFF (YEAR, T.dwoDOB, CURRENT_TIMESTAMP), T.dwoDOB) > CURRENT_TIMESTAMP THEN DATEDIFF(YEAR, T.dwoDOB, CURRENT_TIMESTAMP) - 1 ELSE DATEDIFF(YEAR, T.dwoDOB, CURRENT_TIMESTAMP) END END
Example
SELECT T.swmFirstName, T.swmLastName, ircAge = CASE WHEN T.dwoDOB IS NULL THEN NULL ELSE CASE WHEN DATEADD(YEAR, DATEDIFF (YEAR, T.dwoDOB, CURRENT_TIMESTAMP), T.dwoDOB) > CURRENT_TIMESTAMP THEN DATEDIFF(YEAR, T.dwoDOB, CURRENT_TIMESTAMP) - 1 ELSE DATEDIFF(YEAR, T.dwoDOB, CURRENT_TIMESTAMP) END END FROM T as tblContacts
0 comments:
Post a Comment