CASE
WHEN RIGHT(
BirthDT7,1) IN ('9','0') THEN '18' + LEFT(
BirthDT7, 6)
WHEN RIGHT(
BirthDT7,1) IN ('1','2') THEN '19' + LEFT(
BirthDT7, 6)
WHEN RIGHT(
BirthDT7,1) IN ('3','4') THEN '20' + LEFT(
BirthDT7, 6)
WHEN RIGHT(
BirthDT7,1) IN ('5','6') THEN '19' + LEFT(
BirthDT7, 6)
WHEN RIGHT(
BirthDT7,1) IN ('7','8') THEN '20' + LEFT(
BirthDT7, 6)
END
select
datediff(yy, left(birthdt,8), getdate()-1)
+ case when right(birthdt, 4) > right(convert(char(8), getdate()-1, 112), 4) then -1 else 0 end age
, gender
from (
select
case when right(@birth7,1) % 2 = 1 then '' else '' end gender
, case
when right(@birth7,1) in (3,4,7,8) then '20' + left(@birth7,6)
when right(@birth7,1) in (1,2,5,6) then '19' + left(@birth7,6)
when right(@birth7,1) in (9,0) then '18' + left(@birth7,6)
end birthdt
) t