0

I am trying to construct a tvf for calculating age. Currently, I am working with this code, which brings up a few questions.

Code:

IF ( MONTH (GETDATE()) = (SELECT MONTH (ISNULL (PERS_DateBirth, '1900-01-01')) 
                          FROM T_Person) 
     AND DAY (GETDATE()) >= (SELECT DAY (ISNULL(PERS_DateBirth, '1900-01-01')) 
                             FROM T_Person)
     OR MONTH (GETDATE()) > (SELECT MONTH (ISNULL (PERS_DateBirth, '1900-01-01')) 
                             FROM T_Person)
   )
SELECT YEAR (GETDATE()) - YEAR (ISNULL (PERS_DateBirth, '1900-01-01')) 
FROM T_Person
ELSE SELECT YEAR (GETDATE()) - YEAR (ISNULL (PERS_DateBirth, '1900-01-01')) - 1  
     FROM T_Person

Questions:

  1. Subquery returned more than 1 value. How is that possible, considering the code above?
  2. Can anyone help me in rewriting this, so that it can be a tvf, maybe using CASE WHEN.

PS. I prefer the construct / logic as above, over DATEDIFF constructions using 365,25 days and so on.

Thanks all for helping out, if the code above is unclear, please let me know.

Ankit Bajpai
  • 13,128
  • 4
  • 25
  • 40
SQL_M
  • 2,455
  • 2
  • 16
  • 30

0 Answers0