select memid
,fullname
,dob
,sex
,enrollid
,enrolleffdate
,enrolltermdate
,(datediff(month,dob,enrolleffdate)+1)/12 as age
,case
when (datediff(month,dob,enrolleffdate)+1)/12<21
then 0
else datediff(month,enrolleffdate,enrolltermdate)
end as membermonth
from @memage
;
Thanks for your reply.
say for example if the enrollment eff date is 01-jun-2015 and enrollment Termdate is 30-Aug-2015. suppose during july only he/she attains the age as 21 then the member month should be 2. in your query the member month will show as 3 months under else part.
select memid
,fullname
,dob
,sex
,enrollid
,enrolleffdate
,enrolltermdate
,(datediff(month,dob,enrolleffdate)+1)/12 as age
,case
when (datediff(month,dob,enrolleffdate)+1)/12<21
then 0
else datediff(month,dob,enrolltermdate)-(21*12)+1
end as membermonth
from @memage
;